aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/neomedia/AbstractDeviceConfigurationListener.java
blob: a1814e8484b36a49751923f7273fc36aa50a0874 (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
192
193
194
195
196
197
/*
 * 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.neomedia;

import java.beans.*;
import java.util.*;

import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.notification.*;
import net.java.sip.communicator.service.systray.event.*;
import net.java.sip.communicator.util.*;

/**
 * An abstract listener to the click on the popup message concerning
 * device configuration changes.
 *
 * @author Vincent Lucas
 */
public abstract class AbstractDeviceConfigurationListener
    implements PropertyChangeListener,
               SystrayPopupMessageListener
{
    /**
     *  The audio or video configuration form.
     */
    private final ConfigurationForm configurationForm;

    /**
     * A boolean used to verify that this listener registers only once to
     * the popup message notification handler.
     */
    private boolean isRegisteredToPopupMessageListener = false;

    /**
     * Creates an abstract listener to the click on the popup message concerning
     * device configuration changes.
     *
     * @param configurationForm The audio or video configuration form.
     */
    public AbstractDeviceConfigurationListener(
            ConfigurationForm configurationForm)
    {
        this.configurationForm = configurationForm;
    }

    /**
     * Adds/removes this instance as a <tt>PopupMessageListener</tt> to/from the
     * <tt>NotificationService</tt> in order to be able to detect when the user
     * clicks on a pop-up notification displayed by this instance.
     *
     * @param add <tt>true</tt> to add this instance as a
     * <tt>PopupMessageListener</tt> to the <tt>NotificationService</tt> or
     * <tt>false</tt> to remove it
     */
    private void addOrRemovePopupMessageListener(boolean add)
    {
        Iterator<NotificationHandler> notificationHandlers
            = NeomediaActivator.getNotificationService()
                    .getActionHandlers(NotificationAction.ACTION_POPUP_MESSAGE)
                        .iterator();

        while(notificationHandlers.hasNext())
        {
            NotificationHandler notificationHandler
                = notificationHandlers.next();

            if(notificationHandler instanceof PopupMessageNotificationHandler)
            {
                PopupMessageNotificationHandler popupMessageNotificationHandler
                    = (PopupMessageNotificationHandler) notificationHandler;

                if(add)
                {
                    popupMessageNotificationHandler.addPopupMessageListener(
                            this);
                }
                else
                {
                    popupMessageNotificationHandler.removePopupMessageListener(
                            this);
                }
            }
        }
    }

    /**
     * Releases the resources acquired by this instance throughout its lifetime,
     * uninstalls the listeners it has installed and, generally, prepares it for
     * garbage collection.
     */
    public void dispose()
    {
        addOrRemovePopupMessageListener(false);
    }

    /**
     * Indicates that user has clicked on the systray popup message.
     *
     * @param ev the event triggered when user clicks on the systray popup
     * message
     */
    public void popupMessageClicked(SystrayPopupMessageEvent ev)
    {
        // Checks if this event is fired from one click on one of our popup
        // message.
        if(ev.getTag() == this)
        {
            // Get the UI service
            UIService uiService
                = ServiceUtils.getService(
                        NeomediaActivator.getBundleContext(),
                        UIService.class);

            if(uiService != null)
            {
                // Shows the audio configuration window.
                ConfigurationContainer configurationContainer
                    = uiService.getConfigurationContainer();

                configurationContainer.setSelected(configurationForm);
                configurationContainer.setVisible(true);
            }
        }
    }

    /**
     * Function called when an audio device is plugged or unplugged.
     *
     * @param ev The property change event which may concern the audio device
     */
    public abstract void propertyChange(PropertyChangeEvent ev);

    /**
     * Shows a pop-up notification corresponding to a device configuration
     * change.
     *
     * @param title The title of the pop-up notification.
     * @param body A body text describing the device modifications.
     * @param popUpEvent The event for a device which has fired this
     * notification: connected, disconnected or selected.
     */
    public void showPopUpNotification(
            String title,
            String body,
            String popUpEvent)
    {
        // Shows the pop-up notification.
        if(title != null && body != null && popUpEvent != null)
        {
            NotificationService notificationService
                = NeomediaActivator.getNotificationService();

            if(notificationService != null)
            {
                // Registers only once to the popup message notification
                // handler.
                if(!isRegisteredToPopupMessageListener)
                {
                    isRegisteredToPopupMessageListener = true;
                    addOrRemovePopupMessageListener(true);
                }

                // Fires the popup notification.
                Map<String,Object> extras = new HashMap<String,Object>();

                extras.put(
                        NotificationData.POPUP_MESSAGE_HANDLER_TAG_EXTRA,
                        this);
                notificationService.fireNotification(
                        popUpEvent,
                        title,
                        body
                        + "\r\n\r\n"
                        + NeomediaActivator.getResources().getI18NString(
                                "impl.media.configform"
                                    + ".AUDIO_DEVICE_CONFIG_MANAGMENT_CLICK"),
                        null,
                        extras);
            }
        }
    }
}