Repository navigation
Expand file tree
/
Copy pathsound.js
More file actions
142 lines (125 loc) · 5.38 KB
/
Copy pathsound.js
File metadata and controls
142 lines (125 loc) · 5.38 KB
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
/* -*- Mode: js; js-indent-level: 2; indent-tabs-mode: nil -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
(function() {
'use strict';
var _ = navigator.mozL10n.get;
// Show the touch tone selector if and only if we're on a CDMA network
getSupportedNetworkInfo(function(result) {
var toneSelector = document.getElementById('touch-tone-selector');
toneSelector.hidden = !result.cdma;
});
// Now initialize the ring tone and alert tone menus.
// This array has one element for each selectable tone that appears in the
// "Tones" section of ../elements/sound.html.
var tones = [
{
pickType: 'alerttone',
settingsKey: 'notification.ringtone',
allowNone: true, // Allow "None" as a choice for alert tones.
button: document.getElementById('alert-tone-selection')
}
];
// If we're a telephone, then show the section for ringtones, too.
if (navigator.mozTelephony) {
tones.push({
pickType: 'ringtone',
settingsKey: 'dialer.ringtone',
allowNone: false, // The ringer must always have an actual sound.
button: document.getElementById('ring-tone-selection')
});
document.getElementById('ringer').hidden = false;
}
// For each kind of tone, hook up the button that will allow the user
// to select a sound for that kind of tone.
tones.forEach(function(tone) {
var namekey = tone.settingsKey + '.name';
// The button looks like a select element. By default it just reads
// "change". But we want it to display the name of the current tone.
// So we look up that name in the settings database.
SettingsListener.observe(namekey, '', function(tonename) {
tone.button.textContent = tonename || _('change');
});
// When the user clicks the button, we launch an activity that lets
// the user select new ringtone.
tone.button. {
// Before we can start the Pick activity, we need to know if there
// is locked content on the phone because we don't want the user to
// see "Purchased Media" as a choice if there isn't any purchased
// media on the phone. The ForwardLock secret key is not generated
// until it is needed, so we can use its existance to determine whether
// to show the Purchased Media app.
ForwardLock.getKey(function(secret) {
var activity = new MozActivity({
name: 'pick',
data: {
type: tone.pickType,
allowNone: tone.allowNone,
// If we have a secret then there is locked content on the phone
// so include it as a choice for the user
includeLocked: (secret !== null)
}
});
activity. {
var blob = activity.result.blob; // The returned ringtone sound
var name = activity.result.name; // The name of this ringtone
if (!blob) {
if (tone.allowNone) {
// If we allow a null blob, then everything is okay
setRingtone(blob, name);
}
else {
// Otherwise this is an error and we should not change the
// current setting. (The ringtones app should never return
// a null blob if allowNone is false, but other apps might.)
alert(_('unplayable-ringtone'));
}
return;
}
// If we got a locked ringtone, we have to unlock it first
if (blob.type.split('/')[1] === ForwardLock.mimeSubtype) {
ForwardLock.unlockBlob(secret, blob, function(unlocked) {
checkRingtone(unlocked, name);
});
} else { // Otherwise we can just use the blob directly.
checkRingtone(blob, name);
}
// Make sure that the blob we got from the activity is actually
// a playable audio file. It would be very bad to set an corrupt
// blob as a ringtone because then the phone wouldn't ring!
function checkRingtone(blob, name) {
var oldRingtoneName = tone.button.textContent;
tone.button.textContent = _('savingringtone');
var player = new Audio();
player.preload = 'metadata';
player.src = URL.createObjectURL(blob);
player. {
release();
setRingtone(blob, name); // this will update the button text
};
player. {
release();
tone.button.textContent = oldRingtoneName;
alert(_('unplayable-ringtone'));
};
function release() {
URL.revokeObjectURL(player.src);
player.removeAttribute('src');
player.load();
}
}
// Save the sound in the settings db so that other apps can use it.
// Also save the sound name in the db so we can display it in the
// future. And update the button text to the new name now.
function setRingtone(blob, name) {
// Update the settings database. This will cause the button
// text to change as well because of the SettingsListener above.
var values = {};
values[tone.settingsKey] = blob;
values[namekey] = name || '';
navigator.mozSettings.createLock().set(values);
}
};
});
};
});
}());