<commit_before>/*
 * SIP Communicator, 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.statusupdate;

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

import net.java.sip.communicator.service.configuration.ConfigurationService;
import net.java.sip.communicator.service.protocol.*;

/**
 * A Runnable, which permanently looks at the mouse position. If the mouse is
 * not moved, all accounts are set to "Away" or similar states.
 * 
 * @author <NAME>
 * 
 */
public class StatusUpdateThread implements Runnable
{
    private boolean run = false;
    private Point lastPosition = null;
    private Map<ProtocolProviderService, PresenceStatus> lastStates = new HashMap<ProtocolProviderService, PresenceStatus>();

    private static final int IDLE_TIMER = 3000;
    private static final int AWAY_DEFAULT_STATUS = 40;

    public void run()
    {
        run = true;
        int timer = 0;
        do
        {
            try
            {
                Point currentPosition = MouseInfo.getPointerInfo()
                        .getLocation();
                if (!isNear(lastPosition, currentPosition))
                {
                    // position has changed
                    // check, if a minor state has been automatically set and
                    // reset this state to the former state.
                    ProtocolProviderService[] pps = StatusUpdateActivator
                            .getProtocolProviders();

                    for (ProtocolProviderService protocolProviderService : pps)
                    {
                        if (lastStates.get(protocolProviderService) != null)
                        {
                            PresenceStatus lastState = lastStates
                                    .get(protocolProviderService);
                            OperationSetPresence presence = (OperationSetPresence) protocolProviderService
                                    .getOperationSet(OperationSetPresence.class);
                            try
                            {
                                presence.publishPresenceStatus(lastState, "");
                            } catch (IllegalArgumentException e)
                            {
                            } catch (IllegalStateException e)
                            {
                            } catch (OperationFailedException e)
                            {
                            }
                            lastStates.remove(protocolProviderService);
                        }
                    }
                    timer = getTimer() * 1000 * 60;
                } else
                {
                    // position has not changed!
                    // get all protocols and set them to away

                    ProtocolProviderService[] pps = StatusUpdateActivator
                            .getProtocolProviders();

                    for (ProtocolProviderService protocolProviderService : pps)
                    {
                        OperationSetPresence presence = (OperationSetPresence) protocolProviderService
                                .getOperationSet(OperationSetPresence.class);

                        PresenceStatus status = presence.getPresenceStatus();

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

                        lastStates.put(protocolProviderService, presence
                                .getPresenceStatus());

                        PresenceStatus newStatus = findAwayStatus(presence);

                        try
                        {
                            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);
    }<commit_msg>Avoid a NPE, which can occour when disconnecting from a remote desktop connection.<commit_after>/*
 * SIP Communicator, 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.statusupdate;

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

import net.java.sip.communicator.service.configuration.ConfigurationService;
import net.java.sip.communicator.service.protocol.*;

/**
 * A Runnable, which permanently looks at the mouse position. If the mouse is
 * not moved, all accounts are set to "Away" or similar states.
 * 
 * @author <NAME>
 * 
 */
public class StatusUpdateThread implements Runnable
{
    private boolean run = false;
    private Point lastPosition = null;
    private Map<ProtocolProviderService, PresenceStatus> lastStates = new HashMap<ProtocolProviderService, PresenceStatus>();

    private static final int IDLE_TIMER = 3000;
    private static final int AWAY_DEFAULT_STATUS = 40;

    public void run()
    {
        run = true;
        int timer = 0;
        do
        {
            try
            {
                if (MouseInfo.getPointerInfo() != null)
                {
                    Point currentPosition = MouseInfo.getPointerInfo()
                            .getLocation();
                    if (!isNear(lastPosition, currentPosition))
                    {
                        // position has changed
                        // check, if a minor state has been automatically set
                        // and
                        // reset this state to the former state.
                        ProtocolProviderService[] pps = StatusUpdateActivator
                                .getProtocolProviders();

                        for (ProtocolProviderService protocolProviderService : pps)
                        {
                            if (lastStates.get(protocolProviderService) != null)
                            {
                                PresenceStatus lastState = lastStates
                                        .get(protocolProviderService);
                                OperationSetPresence presence = (OperationSetPresence) protocolProviderService
                                        .getOperationSet(OperationSetPresence.class);
                                try
                                {
                                    presence.publishPresenceStatus(lastState,
                                            "");
                                } catch (IllegalArgumentException e)
                                {
                                } catch (IllegalStateException e)
                                {
                                } catch (OperationFailedException e)
                                {
                                }
                                lastStates.remove(protocolProviderService);
                            }
                        }
                        timer = getTimer() * 1000 * 60;
                    } else
                    {
                        // position has not changed!
                        // get all protocols and set them to away

                        ProtocolProviderService[] pps = StatusUpdateActivator
                                .getProtocolProviders();

                        for (ProtocolProviderService protocolProviderService : pps)
                        {
                            OperationSetPresence presence = (OperationSetPresence) protocolProviderService
                                    .getOperationSet(OperationSetPresence.class);

                            PresenceStatus status = presence
                                    .getPresenceStatus();

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

                            lastStates.put(protocolProviderService, presence
                                    .getPresenceStatus());

                            PresenceStatus newStatus = findAwayStatus(presence);

                            try
                            {
                                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);
    }
{'fraction_non_alphanumeric': 0.059755592377796186, 'fraction_numerical': 0.002899751449875725, 'mean_word_length': 1.060819462227913, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '831557', 'n_tokens_mistral': 2031, 'n_tokens_neox': 1865, 'n_words': 481}