aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/desktoputil/SoundLevelIndicator.java
blob: e0d4b3b399fed02f35d92878368050383eb05a52 (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
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
/*
 * 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.plugin.desktoputil;

import java.awt.*;

import javax.swing.*;

import net.java.sip.communicator.util.skin.*;

import org.jitsi.service.resources.*;

/**
 * Represents the sound level indicator for a particular peer.
 *
 * @author Dilshan Amadoru
 * @author Yana Stamcheva
 * @author Adam Netocny
 * @author Lyubomir Marinov
 */
public class SoundLevelIndicator
    extends TransparentPanel
    implements Skinnable
{
    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = 0L;

    private static final String SOUND_LEVEL_ACTIVE_LEFT
        = "service.gui.soundlevel.SOUND_LEVEL_ACTIVE_LEFT";

    private static final String SOUND_LEVEL_ACTIVE_LEFT_GRADIENT
        = "service.gui.soundlevel.SOUND_LEVEL_ACTIVE_LEFT_GRADIENT";

    private static final String SOUND_LEVEL_ACTIVE_MIDDLE
        = "service.gui.soundlevel.SOUND_LEVEL_ACTIVE_MIDDLE";

    private static final String SOUND_LEVEL_ACTIVE_RIGHT
        = "service.gui.soundlevel.SOUND_LEVEL_ACTIVE_RIGHT";

    private static final String SOUND_LEVEL_ACTIVE_RIGHT_GRADIENT
        = "service.gui.soundlevel.SOUND_LEVEL_ACTIVE_RIGHT_GRADIENT";

    private static final String SOUND_LEVEL_INACTIVE_LEFT
        = "service.gui.soundlevel.SOUND_LEVEL_INACTIVE_LEFT";

    private static final String SOUND_LEVEL_INACTIVE_MIDDLE
        = "service.gui.soundlevel.SOUND_LEVEL_INACTIVE_MIDDLE";

    private static final String SOUND_LEVEL_INACTIVE_RIGHT
        = "service.gui.soundlevel.SOUND_LEVEL_INACTIVE_RIGHT";

    /**
     * A runnable that will be used to update the sound level.
     */
    private final LevelUpdate levelUpdate = new LevelUpdate();

    /**
     * The <tt>Runnable</tt> which schedules the execution of
     * {@link #levelUpdate}. Introduced to better the garbage collection profile
     * of the utilization of <tt>LowPriorityEventQueue</tt>.
     *
     * @see LowPriorityEventQueue#createRepetitiveInvokeLater(Runnable)
     */
    private Runnable levelUpdateScheduler;

    /**
     * The maximum possible sound level.
     */
    private final int maxSoundLevel;

    /**
     * The minimum possible sound level.
     */
    private final int minSoundLevel;

    /**
     * The number of (distinct) sound bars displayed by this instance.
     */
    private int soundBarCount;

    /**
     * The sound level which is currently depicted by this
     * <tt>SoundLevelIndicator</tt>.
     */
    private int soundLevel;

    /**
     * Image when a sound level block is active
     */
    private ImageIcon soundLevelActiveImageLeft;

    /**
     * Image when a sound level block is active
     */
    private ImageIcon soundLevelActiveImageLeftGradient;

    /**
     * Image when a sound level block is active
     */
    private ImageIcon soundLevelActiveImageMiddle;

    /**
     * Image when a sound level block is active
     */
    private ImageIcon soundLevelActiveImageRight;

    /**
     * Image when a sound level block is active
     */
    private ImageIcon soundLevelActiveImageRightGradient;

    /**
     * Image when a sound level block is not active
     */
    private ImageIcon soundLevelInactiveImageLeft;

    /**
     * Image when a sound level block is not active
     */
    private ImageIcon soundLevelInactiveImageMiddle;

    /**
     * Image when a sound level block is not active
     */
    private ImageIcon soundLevelInactiveImageRight;

    /**
     * Initializes a new <tt>SoundLevelIndicator</tt> instance.
     *
     * @param minSoundLevel the minimum possible sound level
     * @param maxSoundLevel the maximum possible sound level
     */
    public SoundLevelIndicator(int minSoundLevel, int maxSoundLevel)
    {
        this.minSoundLevel = minSoundLevel;
        this.maxSoundLevel = maxSoundLevel;
        this.soundLevel = minSoundLevel;

        loadSkin();

        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    }

    /**
     * Returns the number of sound level bars that we could currently show in
     * this panel.
     *
     * @param width the current width of the call window
     * @return the number of sound level bars that we could currently show in
     * this panel
     */
    private int getSoundBarCount(int width)
    {
        int soundBarWidth = soundLevelActiveImageLeft.getIconWidth();

        return width / soundBarWidth;
    }

    /**
     * Reloads icons.
     */
    public void loadSkin()
    {
        ResourceManagementService resources = DesktopUtilActivator.getResources();

        soundLevelActiveImageLeft
            = resources.getImage(SOUND_LEVEL_ACTIVE_LEFT);
        soundLevelActiveImageLeftGradient
            = resources.getImage(SOUND_LEVEL_ACTIVE_LEFT_GRADIENT);
        soundLevelActiveImageMiddle
            = resources.getImage(SOUND_LEVEL_ACTIVE_MIDDLE);
        soundLevelActiveImageRight
            = resources.getImage(SOUND_LEVEL_ACTIVE_RIGHT);
        soundLevelActiveImageRightGradient
            = resources.getImage(SOUND_LEVEL_ACTIVE_RIGHT_GRADIENT);

        soundLevelInactiveImageLeft
            = resources.getImage(SOUND_LEVEL_INACTIVE_LEFT);
        soundLevelInactiveImageMiddle
            = resources.getImage(SOUND_LEVEL_INACTIVE_MIDDLE);
        soundLevelInactiveImageRight
            = resources.getImage(SOUND_LEVEL_INACTIVE_RIGHT);

        if (!isPreferredSizeSet())
        {
            int preferredHeight = 0;
            int preferredWidth = 0;

            if (soundLevelActiveImageLeft != null)
            {
                int height = soundLevelActiveImageLeft.getIconHeight();
                int width = soundLevelActiveImageLeft.getIconWidth();

                if (preferredHeight < height)
                    preferredHeight = height;
                if (preferredWidth < width)
                    preferredWidth = width;
            }
            if (soundLevelInactiveImageLeft != null)
            {
                int height = soundLevelInactiveImageLeft.getIconHeight();
                int width = soundLevelInactiveImageLeft.getIconWidth();

                if (preferredHeight < height)
                    preferredHeight = height;
                if (preferredWidth < width)
                    preferredWidth = width;
            }
            if ((preferredHeight > 0) && (preferredWidth > 0))
                setPreferredSize(
                        new Dimension(
                                10 * preferredWidth,
                                preferredHeight));
        }

        updateSoundLevel(soundLevel);
    }

    public void resetSoundLevel()
    {
        soundLevel = minSoundLevel;
        updateSoundLevel(minSoundLevel);
    }

    @Override
    public void setBounds(int x, int y, int width, int height)
    {
        super.setBounds(x, y, width, height);

        int newSoundBarCount = getSoundBarCount(getWidth());

        if (newSoundBarCount > 0)
        {
            while (newSoundBarCount < soundBarCount)
            {
                for (int i = getComponentCount() - 1; i >= 0; i--)
                {
                    Component c = getComponent(i);

                    if (c instanceof JLabel)
                    {
                        remove(c);
                        soundBarCount--;
                        break;
                    }
                }
            }
            while (soundBarCount < newSoundBarCount)
            {
                JLabel soundBar;
                if (soundBarCount == 0)
                    soundBar = new JLabel(soundLevelInactiveImageLeft);
                else if (soundBarCount == newSoundBarCount - 1)
                    soundBar = new JLabel(soundLevelInactiveImageRight);
                else
                    soundBar = new JLabel(soundLevelInactiveImageMiddle);

                soundBar.setVerticalAlignment(JLabel.CENTER);
                add(soundBar);
                soundBarCount++;
            }
        }

        updateSoundLevel(soundLevel);
        revalidate();
        repaint();
    }

    /**
     * Update the sound level indicator component to fit the given values.
     *
     * @param soundLevel the sound level to show
     */
    public void updateSoundLevel(int soundLevel)
    {
        levelUpdate.setSoundLevel(soundLevel);

        Runnable levelUpdateScheduler;

        synchronized (this)
        {
            if (this.levelUpdateScheduler == null)
            {
                this.levelUpdateScheduler
                    = LowPriorityEventQueue.createRepetitiveInvokeLater(
                            levelUpdate);
            }
            levelUpdateScheduler = this.levelUpdateScheduler;
        }

        levelUpdateScheduler.run();
    }

    /**
     * Update the sound level indicator component to fit the given values.
     *
     * @param soundLevel the sound level to show
     */
    private void updateSoundLevelInternal(int soundLevel)
    {
        int range = 1;

        // Check if the given range values are correct.
        if ((minSoundLevel > -1)
                && (maxSoundLevel > -1)
                && (minSoundLevel < maxSoundLevel))
        {
            range = maxSoundLevel - minSoundLevel;

            if (soundLevel < 40 /* A WHISPER */)
                soundLevel = minSoundLevel;
            else if (soundLevel > 85 /* BEGINNING OF HEARING DAMAGE */)
                soundLevel = maxSoundLevel;
            else
            {
                /*
                 * Depict the range between "A WHISPER" and "BEGINNING OF
                 * HEARING DAMAGE".
                 */
                soundLevel = (int) (((soundLevel - 40.0) / 45.0) * range);
                if (soundLevel < minSoundLevel)
                    soundLevel = minSoundLevel;
                else if (soundLevel > maxSoundLevel)
                    soundLevel = maxSoundLevel;
            }
        }

        /*
         * Audacity uses 0.9 for this.soundLevel and, consequently, 0.1 for
         * soundLevel but that makes the animation too slow.
         */
        this.soundLevel = (int) (this.soundLevel * 0.8 + soundLevel * 0.2);

        int activeSoundBarCount
            = Math.round(this.soundLevel * soundBarCount / (float) range);

        /*
         * We cannot use getComponentCount() and then call getComponent(int)
         * because there are multiple threads involved and the code bellow is
         * not executed on the UI thread i.e. ArrayIndexOutOfBounds may and do
         * happen.
         */
        Component[] components = getComponents();

        for (int i = 0; i < components.length; i++)
        {
            Component c = getComponent(i);

            if (c instanceof JLabel)
            {
                Icon activeIcon = null;
                Icon inactiveIcon = null;
                if (i == 0)
                {
                    if (activeSoundBarCount == 1)
                        activeIcon = soundLevelActiveImageLeftGradient;
                    else
                    {
                        activeIcon = soundLevelActiveImageLeft;
                        inactiveIcon = soundLevelInactiveImageLeft;
                    }
                }
                else if (i == activeSoundBarCount - 1)
                {
                    if (i == components.length - 1)
                        activeIcon = soundLevelActiveImageRight;
                    else
                        activeIcon = soundLevelActiveImageRightGradient;
                }
                else if (i == components.length - 1)
                {
                    inactiveIcon = soundLevelInactiveImageRight;
                }
                else
                {
                    activeIcon = soundLevelActiveImageMiddle;
                    inactiveIcon = soundLevelInactiveImageMiddle;
                }

                ((JLabel) c).setIcon(
                        (i < activeSoundBarCount)
                            ? activeIcon
                            : inactiveIcon);
            }
        }

        repaint();
    };

    /**
     * Runnable used to update sound levels.
     */
    private class LevelUpdate
        implements Runnable
    {
        /**
         * The current sound level to update.
         */
        private int soundLevel;

        /**
         * Update.
         */
        public void run()
        {
            updateSoundLevelInternal(soundLevel);
        }

        /**
         * Changes the sound level.
         * @param soundLevel changes the sound level.
         */
        public void setSoundLevel(int soundLevel)
        {
            this.soundLevel = soundLevel;
        }
    }
}