diff options
author | Ted Gould <ted@gould.cx> | 2014-10-14 12:08:00 -0500 |
---|---|---|
committer | Ted Gould <ted@gould.cx> | 2014-10-14 12:08:00 -0500 |
commit | 2b4342f72a57c6b3d112fd6e923c544be789f6ea (patch) | |
tree | e89cd0ad054e3a94475661a67d6d89a4260caf9c | |
parent | a7ad5e0c977fbd4d1f0a3efa071bcffd0e5d6b13 (diff) | |
parent | 8e7cbaadfed452063b2d659bc679746cad5cbb14 (diff) | |
download | ayatana-indicator-sound-2b4342f72a57c6b3d112fd6e923c544be789f6ea.tar.gz ayatana-indicator-sound-2b4342f72a57c6b3d112fd6e923c544be789f6ea.tar.bz2 ayatana-indicator-sound-2b4342f72a57c6b3d112fd6e923c544be789f6ea.zip |
Grabbing the extreme volume warnings
-rw-r--r-- | src/service.vala | 14 | ||||
-rw-r--r-- | src/sound-menu.vala | 20 | ||||
-rw-r--r-- | src/volume-control.vala | 80 | ||||
-rw-r--r-- | tests/manual | 35 |
4 files changed, 148 insertions, 1 deletions
diff --git a/src/service.vala b/src/service.vala index 4e02769..94b0645 100644 --- a/src/service.vala +++ b/src/service.vala @@ -36,6 +36,7 @@ public class IndicatorSound.Service: Object { this.actions.add_action (this.create_mute_action ()); this.actions.add_action (this.create_volume_action ()); this.actions.add_action (this.create_mic_volume_action ()); + this.actions.add_action (this.create_high_volume_actions ()); this.menus = new HashTable<string, SoundMenu> (str_hash, str_equal); this.menus.insert ("desktop_greeter", new SoundMenu (null, SoundMenu.DisplayFlags.SHOW_MUTE | SoundMenu.DisplayFlags.HIDE_PLAYERS)); @@ -47,6 +48,10 @@ public class IndicatorSound.Service: Object { this.volume_control.bind_property ("active-mic", menu, "show-mic-volume", BindingFlags.SYNC_CREATE); }); + this.menus.@foreach ( (profile, menu) => { + this.volume_control.bind_property ("high-volume", menu, "show-high-volume-warning", BindingFlags.SYNC_CREATE); + }); + /* Setup handling for the greeter-export setting */ this.settings.changed["greeter-export"].connect( () => this.build_accountsservice() ); build_accountsservice(); @@ -365,6 +370,15 @@ public class IndicatorSound.Service: Object { return volume_action; } + Action create_high_volume_actions () { + var high_volume_action = new SimpleAction.stateful("high-volume", null, new Variant.boolean (this.volume_control.high_volume)); + + this.volume_control.notify["high_volume"].connect( () => + high_volume_action.set_state(new Variant.boolean (this.volume_control.high_volume))); + + return high_volume_action; + } + void bus_acquired (DBusConnection connection, string name) { try { connection.export_action_group ("/com/canonical/indicator/sound", this.actions); diff --git a/src/sound-menu.vala b/src/sound-menu.vala index 03faa89..38c5214 100644 --- a/src/sound-menu.vala +++ b/src/sound-menu.vala @@ -82,12 +82,31 @@ public class SoundMenu: Object this.mic_volume_shown = true; } else if (!value && this.mic_volume_shown) { + /* TODO: Make smarter */ this.volume_section.remove (this.volume_section.get_n_items () -1); this.mic_volume_shown = false; } } } + public bool show_high_volume_warning { + get { + return this.high_volume_warning_shown; + } + set { + if (value && !this.high_volume_warning_shown) { + var item = new MenuItem(_("High volume can damage your hearing."), null); + volume_section.append_item (item); + this.high_volume_warning_shown = true; + } + else if (!value && this.high_volume_warning_shown) { + /* TODO: Make smarter */ + this.volume_section.remove (this.volume_section.get_n_items () -1); + this.high_volume_warning_shown = false; + } + } + } + public void add_player (MediaPlayer player) { if (this.notify_handlers.contains (player)) return; @@ -130,6 +149,7 @@ public class SoundMenu: Object Menu volume_section; bool mic_volume_shown; bool settings_shown = false; + bool high_volume_warning_shown = false; bool hide_inactive; bool hide_players = false; HashTable<MediaPlayer, ulong> notify_handlers; diff --git a/src/volume-control.vala b/src/volume-control.vala index ba87de5..8e92f90 100644 --- a/src/volume-control.vala +++ b/src/volume-control.vala @@ -19,6 +19,7 @@ */ using PulseAudio; +using Notify; using Gee; [CCode(cname="pa_cvolume_set", cheader_filename = "pulse/volume.h")] @@ -67,6 +68,9 @@ public class VolumeControl : Object private uint _accountservice_volume_timer = 0; private bool _send_next_local_volume = false; private double _account_service_volume = 0.0; + private Notify.Notification _notification; + + private bool _active_port_headphone = false; public signal void volume_changed (double v); public signal void mic_volume_changed (double v); @@ -77,6 +81,9 @@ public class VolumeControl : Object /** true when a microphone is active **/ public bool active_mic { get; private set; default = false; } + /** true when high volume warnings should be shown */ + public bool high_volume { get; set; } + public VolumeControl () { if (loop == null) @@ -84,6 +91,13 @@ public class VolumeControl : Object _mute_cancellable = new Cancellable (); _volume_cancellable = new Cancellable (); + + Notify.init ("Volume"); + _notification = new Notify.Notification(_("Volume"), "", "audio-volume-muted"); + _notification.set_hint ("value", 0); + _notification.set_hint ("x-canonical-private-synchronous", "true"); + _notification.set_hint ("x-canonical-non-shaped-icon", "true"); + setup_accountsservice.begin (); this.reconnect_to_pulse (); @@ -149,6 +163,8 @@ public class VolumeControl : Object private void sink_info_cb_for_props (Context c, SinkInfo? i, int eol) { + bool old_active_port_headphone = this._active_port_headphone; + if (i == null) return; @@ -165,11 +181,26 @@ public class VolumeControl : Object this.notify_property ("is-playing"); } + /* Check if the current active port is headset/headphone */ + /* There is not easy way to check if the port is a headset/headphone besides + * checking for the port name. On touch (with the pulseaudio droid element) + * the headset/headphone port is called 'output-headset' and 'output-headphone'. + * On the desktop this is usually called 'analog-output-headphones' */ + if (i.active_port.name == "output-wired_headset" || + i.active_port.name == "output-wired_headphone" || + i.active_port.name == "analog-output-headphones") { + _active_port_headphone = true; + } else { + _active_port_headphone = false; + } + if (_pulse_use_stream_restore == false && _volume != volume_to_double (i.volume.max ())) { _volume = volume_to_double (i.volume.max ()); volume_changed (_volume); + } else if (this._active_port_headphone != old_active_port_headphone) { + volume_changed (_volume); } } @@ -568,8 +599,55 @@ public class VolumeControl : Object public void set_volume (double volume) { - if (set_volume_internal (volume)) + /* Using this to detect whether we're on the phone or not */ + if (_pulse_use_stream_restore) { + /* Watch for extreme */ + if (volume > 0.75 && _active_port_headphone) + high_volume = true; + else + high_volume = false; + + /* Determine Label */ + string volume_label = _("Volume"); + if (high_volume) + volume_label = _("High volume"); + + /* Choose an icon */ + string icon = "audio-volume-muted"; + if (volume > 0.0 && volume <= 0.33) + icon = "audio-volume-low"; + if (volume > 0.33 && volume <= 0.66) + icon = "audio-volume-medium"; + if (volume > 0.66 && volume <= 1.0) + icon = "audio-volume-high"; + + /* Choose a sound */ + string? sound = null; + if (!((_active_sink_input in _sink_input_list) && (_valid_roles[_active_sink_input] == "multimedia"))) + sound = "/usr/share/sounds/ubuntu/stereo/message.ogg"; + + /* Check tint */ + string tint = "false"; + if (high_volume) + tint = "true"; + + /* Put it all into the notification */ + _notification.update (_("Volume"), volume_label, icon); + _notification.set_hint ("value", (int32)(volume * 100.0)); + _notification.set_hint ("sound-file", sound); + _notification.set_hint ("x-canonical-value-bar-tint", tint); + + /* Show it */ + try { + _notification.show (); + } catch (GLib.Error e) { + warning("Unable to send volume change notification: %s", e.message); + } + } + + if (set_volume_internal (volume)) { start_local_volume_timer(); + } } void set_mic_volume_success_cb (Context c, int success) diff --git a/tests/manual b/tests/manual index 201465c..f69cf14 100644 --- a/tests/manual +++ b/tests/manual @@ -22,3 +22,38 @@ Test-case indicator-sound/unity8-items-check <dd>The menu is populated with items</dd> </dl> +Test-case indicator-sound/unity8-sound-notifications +<dl> + <dt>Adjust volume using HW keys if available</dt> + <dd>A notification bubble should appear with the sound volume</dd> + <dd>An audibule sound should play at the level of the audio</dd> + <dt>Adjust volume with slider in sound indicator</dt> + <dd>A notification bubble should appear with the sound volume</dd> + <dd>An audibule sound should play at the level of the audio</dd> + <dt>Open a video with sound and play in media player</dt> + <dd>The video should play and the sound should be audible</dd> + <dt>Adjust volume using HW keys if available</dt> + <dd>A notification bubble should appear with the sound volume</dd> + <dd>No notification sound should be heard</dd> + <dt>Adjust volume with slider in sound indicator</dt> + <dd>A notification bubble should appear with the sound volume</dd> + <dd>No notification sound should be heard</dd> +</dl> + +Test-case indicator-sound/unity8-high-volume +<dl> + <dt>Plug headphones into the headphone jack</dt> + <dt>Adjust volume so that it is at the midpoint of volume range</dt> + <dd>The slider should be in the middle of the scale</dd> + <dt>Increase the volume once using HW keys if available</dt> + <dd>A notification bubble should appear with the sound volume</dd> + <dd>The text on the notification should read "Volume"</dd> + <dt>Increase the volume using HW keys until it is roughly 90% of the range</dt> + <dd>A notification bubble should appear with the sound volume</dd> + <dd>The text on the notification should read "High volume"</dd> + <dd>The range on the notification bubble should have a different color signifying the higher volume</dd> + <dt>Decrease the volume using HW keys until it is roughly 50% of the range</dt> + <dd>A notification bubble should appear with the sound volume</dd> + <dd>The text on the notification should read "Volume"</dd> + <dd>The range on the notification bubble should have a standard color</dd> +</dl> |