Skip to content

Commit

Permalink
Bug 810780 - Volume rocker, r=timdream, a=blocking-basecamp
Browse files Browse the repository at this point in the history
  • Loading branch information
alivedise committed Dec 10, 2012
1 parent a34e199 commit 57adf12
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 65 deletions.
7 changes: 0 additions & 7 deletions apps/settings/index.html
Expand Up @@ -986,13 +986,6 @@ <h2 data-l10n-id="volume">Volume</h2>
<span class="range-icons volume"></span>
</label>
</li>
<li>
<p data-l10n-id="content-sound">Content</p>
<label>
<input type="range" name="audio.volume.content" step="1" min="0" value="15" max="15">
<span class="range-icons volume"></span>
</label>
</li>
</ul>
<header>
Expand Down
138 changes: 80 additions & 58 deletions apps/system/js/sound_manager.js
Expand Up @@ -19,45 +19,38 @@
}
});

// Store the current active channel; change with 'audio-channel-changed' mozChromeEvent
var currentChannel = 'notification';

var vibrationEnabled = true;

// This event is generated in shell.js in response to bluetooth headset.
// Bluetooth headset always assign audio volume to a specific value when
// pressing its volume-up/volume-down buttons.
window.addEventListener('mozChromeEvent', function(e) {
var type = e.detail.type;
if (type == 'bluetooth-volumeset') {
changeVolume(e.detail.value - currentVolume['bt_sco'], 'bt_sco');
} else if (type == 'audio-channel-changed') {
currentChannel = e.detail.channel;
}
});

// XXX: This workaround could be removed once
// https://bugzilla.mozilla.org/show_bug.cgi?id=811222 landed
function onCall() {
var telephony = window.navigator.mozTelephony;
if (!telephony)
return false;

return telephony.calls.some(function callIterator(call) {
return (call.state == 'connected');
});
};
if (currentChannel == 'telephony')
return true;

// XXX: This workaround could be removed once
// https://bugzilla.mozilla.org/show_bug.cgi?id=811222 landed
function onRing() {
// XXX: This work should be removed
// once we could get telephony channel change event
// https://bugzilla.mozilla.org/show_bug.cgi?id=819858
var telephony = window.navigator.mozTelephony;
if (!telephony)
return false;

return telephony.calls.some(function callIterator(call) {
return (call.state == 'incoming');
return (call.state == 'connected');
});
}

// XXX: This workaround could be removed once
// https://bugzilla.mozilla.org/show_bug.cgi?id=811222 landed
function onNotificationPlaying() {
return false;
}

function onBTEarphoneConnected() {
var bluetooth = navigator.mozBluetooth;
Expand Down Expand Up @@ -102,41 +95,64 @@
currentVolume[channel] = parseInt(Math.max(0, Math.min(max, volume)), 10);
});
}

SettingsListener.observe('vibration.enabled', true, function(vibration) {
if (pendingRequestCount)
return;

vibrationEnabled = vibration;
});

var activeTimeout = 0;

function changeVolume(delta, channel) {
// XXX: These status-checking functions could be removed when
// Bug 811222 landed
if (!channel) {
if (onNotificationPlaying()) {
channel = 'notification';
} else if (onRing()) {
channel = 'notification';
} else if (onCall()) {
channel = 'telephony';
} else {
channel = 'content';
}
// When hardware volume key is pressed, we need to decide which channel we should toggle.
// This method returns the string for setting key 'audio.volume.*' represents that.
// Note: this string does not always equal to currentChannel
// since some different channels are grouped together to listen to the same setting.
function getChannel() {
if (onCall())
return 'telephony';

switch (currentChannel) {
case 'normal':
case 'content':
return 'content';
case 'telephony':
return 'telephony';
case 'alarm':
return 'alarm';
case 'notification':
case 'ringer':
default:
return 'notification';
}
}

if (currentVolume[channel] == 0 ||
((currentVolume[channel] + delta) <= 0)) {
if (delta < 0) {
if (muteState == 'OFF') {
muteState = 'VIBRATION';
} else {
muteState = 'MUTE';
function getVolumeState(currentVolume, delta, channel) {
if (channel == 'notification') {
if (currentVolume + delta <= 0) {
if (currentVolume == 0 && vibrationEnabled) {
vibrationEnabled = false;
} else if (currentVolume > 0 && !vibrationEnabled) {
vibrationEnabled = true;
}
return 'MUTE';
} else {
if (muteState == 'MUTE') {
delta = 0;
muteState = 'VIBRATION';
} else {
muteState = 'OFF';
}
return 'OFF';
}
} else {
if (currentVolume + delta <= 0) {
return 'MUTE';
} else {
return 'OFF';
}
}
}

function changeVolume(delta, channel) {
channel = channel ? channel : getChannel();

muteState = getVolumeState(currentVolume[channel], delta, channel);

var volume = currentVolume[channel] + delta;

Expand All @@ -149,22 +165,28 @@

switch (muteState) {
case 'OFF':
classes.remove('vibration');
classes.remove('mute');
break;
case 'VIBRATION':
classes.add('vibration');
classes.add('mute');
SettingsListener.getSettingsLock().set({
'vibration.enabled': true
});
if (vibrationEnabled) {
classes.add('vibration');
} else {
classes.remove('vibration');
}
break;
case 'MUTE':
classes.remove('vibration');
classes.add('mute');
SettingsListener.getSettingsLock().set({
'vibration.enabled': false
});
if (channel == 'notification') {
if (vibrationEnabled) {
classes.add('vibration');
SettingsListener.getSettingsLock().set({
'vibration.enabled': true
});
} else {
classes.remove('vibration');
SettingsListener.getSettingsLock().set({
'vibration.enabled': false
});
}
}
break;
}

Expand Down
5 changes: 5 additions & 0 deletions apps/system/style/sound_manager/sound_manager.css
Expand Up @@ -27,6 +27,7 @@
}

#volume span.vibration {
visibility: hidden;
margin-left: 5px;
background-image: url('images/vibration_disabled_icon.png');
}
Expand All @@ -52,6 +53,10 @@
display: block;
}

#volume[data-channel="notification"] span.vibration {
visibility: visible;
}

#volume[data-channel="bt_sco"] div:nth-child(n+4),
#volume[data-channel="content"] div:nth-child(n+4),
#volume[data-channel="alarm"] div:nth-child(n+4),
Expand Down

0 comments on commit 57adf12

Please sign in to comment.