aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/generalconfig/autoaway/StatusUpdateThread.java
blob: cc927e57753e37e54c185baf3bdb80b188fed51f (plain)
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
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.plugin.generalconfig.autoaway;

import java.awt.*;
import java.util.*;

import net.java.sip.communicator.plugin.generalconfig.*;
import net.java.sip.communicator.service.protocol.*;

/**
 * A <tt>Runnable</tt> which permanently looks at the mouse position. If the
 * mouse is not moved, all accounts are set to &quot;Away&quot; or similar
 * states.
 *
 * @author Thomas Hofer
 */
public class StatusUpdateThread
    implements Runnable
{
    private static final int AWAY_DEFAULT_STATUS = 40;

    private static final int IDLE_TIMER = 3000;

    /**
     * Finds the Away-Status of the protocols
     *
     * @param presence
     * @return
     */
    static PresenceStatus findAwayStatus(OperationSetPresence presence)
    {
        Iterator<PresenceStatus> statusSet = presence.getSupportedStatusSet();
        PresenceStatus status = null;

        while (statusSet.hasNext())
        {
            PresenceStatus possibleState = statusSet.next();
            int possibleStatus = possibleState.getStatus();

            if ((possibleStatus < PresenceStatus.AVAILABLE_THRESHOLD)
                    && (possibleStatus >= PresenceStatus.ONLINE_THRESHOLD))
            {
                if (status == null
                        || (Math.abs(possibleStatus - AWAY_DEFAULT_STATUS)
                                < Math.abs(
                                        status.getStatus()
                                            - AWAY_DEFAULT_STATUS)))
                {
                    status = possibleState;
                }
            }
        }
        return status;
    }

    private static boolean isNear(Point p1, Point p2)
    {
        return
            (p1 != null)
                && (p2 != null)
                && (Math.abs(p1.x - p2.x) <= 10)
                && (Math.abs(p1.y - p2.y) <= 10);
    }

    private Point lastPosition = null;

    private final Map<ProtocolProviderService, PresenceStatus> lastStates
        = new HashMap<ProtocolProviderService, PresenceStatus>();

    private boolean run = false;

    public boolean isRunning()
    {
        return run;
    }

    public void run()
    {
        run = true;
        int timer = 0;
        do
        {
            try
            {
                if (MouseInfo.getPointerInfo() != null)
                {
                    PointerInfo info = MouseInfo.getPointerInfo();
                    Point currentPosition
                        = (info != null) ? info.getLocation() : new Point(0, 0);

                    if (!isNear(lastPosition, currentPosition))
                    {
                        // Position has changed check, if a minor state has been
                        // automatically set and reset this state to the former
                        // state.
                        for (ProtocolProviderService protocolProvider
                                : GeneralConfigPluginActivator.getProtocolProviders())
                        {
                            if (lastStates.get(protocolProvider) != null)
                            {
                                PresenceStatus lastState
                                    = lastStates.get(protocolProvider);
                                OperationSetPresence presence
                                    = protocolProvider.getOperationSet(
                                            OperationSetPresence.class);
                                try
                                {
                                    presence
                                        .publishPresenceStatus(lastState, "");
                                }
                                catch (IllegalArgumentException e)
                                {
                                }
                                catch (IllegalStateException e)
                                {
                                }
                                catch (OperationFailedException e)
                                {
                                }
                                lastStates.remove(protocolProvider);
                            }
                        }
                        timer = Preferences.getTimer() * 1000 * 60;
                    }
                    else
                    {
                        // Position has not changed! Get all protocols and set
                        // them to away.
                        for (ProtocolProviderService protocolProvider
                                : GeneralConfigPluginActivator.getProtocolProviders())
                        {
                            OperationSetPresence presence
                                = protocolProvider.getOperationSet(
                                        OperationSetPresence.class);
                            PresenceStatus status
                                = presence.getPresenceStatus();

                            if (status.getStatus()
                                    < PresenceStatus.AVAILABLE_THRESHOLD)
                            {
                                // already (manually) set to away or lower
                                continue;
                            }

                            lastStates.put(protocolProvider, status);

                            PresenceStatus newStatus = findAwayStatus(presence);

                            try
                            {
                                if (newStatus != null)
                                {
                                    presence.publishPresenceStatus(
                                            newStatus,
                                            newStatus.getStatusName());
                                }
                            }
                            catch (IllegalArgumentException e)
                            {
                            }
                            catch (IllegalStateException e)
                            {
                            }
                            catch (OperationFailedException e)
                            {
                            }
                        }

                        timer = IDLE_TIMER;
                    }
                    lastPosition = currentPosition;
                }
                Thread.sleep(timer);
            }
            catch (InterruptedException e)
            {
            }
        }
        while (run && timer > 0);
    }

    public void stop()
    {
        run = false;
    }
}