Repository navigation
Expand file tree
/
Copy pathpick.js
More file actions
171 lines (153 loc) · 5.01 KB
/
Copy pathpick.js
File metadata and controls
171 lines (153 loc) · 5.01 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
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
// Wait until we're loaded, localized, and get an activity request
window.addEventListener('load', function() {
navigator.mozL10n.ready(function() {
navigator.mozSetMessageHandler('activity', function(activity) {
var type = activity.source.data.type;
if (type === 'ringtone' ||
(Array.isArray(type) && type.indexOf('ringtone') !== -1)) {
pickRingtone(activity);
}
else if (type === 'wallpaper' ||
(Array.isArray(type) && type.indexOf('wallpaper') !== -1)) {
pickWallpaper(activity);
}
else {
console.error('unexpected activity request',
activity.source.name,
JSON.stringify(activity.source.data));
}
});
});
});
function pickRingtone(activity) {
var selectedRingtone;
var player = new Audio(); // for previewing sounds
var currentRingtoneName;
var numRingtones = 0;
var template = $('ringtone-template');
var container = $('ringtones');
container.hidden = false;
// Display the right title
$('title').textContent = _('pick-ringtone');
// Make the cancel button work
$('back'). {
player.pause(); // Stop any currently playing sound.
activity.postError('cancelled');
};
// Make the done button work
$('done'). {
player.pause(); // Stop any currently playing sound.
// Lock the ringtone and return it as the activity result
ForwardLock.getKey(function(secret) {
ForwardLock.lockBlob(secret, selectedRingtone.blob, {},
function(lockedBlob) {
activity.postResult({
blob: lockedBlob,
name: selectedRingtone.descriptor.name
});
});
});
};
getCurrentRingtoneName(function(name) {
currentRingtoneName = name;
enumerateAndBuildUI();
});
function getCurrentRingtoneName(callback) {
navigator.mozSettings
.createLock()
.get(RINGTONE_NAME_KEY)
. {
callback(e.target.result[RINGTONE_NAME_KEY]);
};
}
function enumerateAndBuildUI() {
// Enumerate the ringtones from the database and display them onscreen
objectStore.readonly('ringtones', function(ringtoneStore) {
var cursor = ringtoneStore.openCursor();
cursor. {
if (cursor.result) {
addRingtone(cursor.result.value);
cursor.result.continue();
}
else { // we reached the end of the enumeration
if (numRingtones === 0)
displayError(activity, 'no-installed-ringtones');
}
};
});
}
function addRingtone(ringtone) {
numRingtones++;
var name = ringtone.descriptor.name;
var dom = template.content.cloneNode(true);
var input = dom.querySelector('input');
dom.querySelector('a').textContent = name;
if (name === currentRingtoneName) {
selectedRingtone = ringtone;
input.checked = true;
}
input. {
if (input.checked) {
selectedRingtone = ringtone;
play(ringtone);
}
};
container.appendChild(dom);
}
function play(ringtone) {
// We create blob urls as needed. Since this app is always short-lived
// we don't bother releasing them.
if (!ringtone.url)
ringtone.url = URL.createObjectURL(ringtone.blob);
player.src = ringtone.url;
player.play();
}
}
function pickWallpaper(activity) {
var numWallpapers = 0;
var template = $('wallpaper-template');
var container = $('wallpapers');
container.hidden = false;
$('title').textContent = _('pick-wallpaper');
$('done').hidden = true;
$('back'). {
activity.postError('cancelled');
};
// Enumerate the wallpapers from the database and display them onscreen
objectStore.readonly('wallpapers', function(wallpaperStore) {
var cursor = wallpaperStore.openCursor();
cursor. {
if (cursor.result) {
addWallpaper(cursor.result.value);
numWallpapers++;
cursor.result.continue();
}
else { // we reached the end of the enumeration
if (numWallpapers === 0)
displayError(activity, 'no-installed-wallpaper');
}
};
});
function addWallpaper(wallpaper) {
var blob = wallpaper.blob;
var url = URL.createObjectURL(blob);
var wallpaper = template.content.cloneNode(true).firstElementChild;
container.appendChild(wallpaper);
wallpaper.style.backgroundImage = 'url(' + url + ')';
wallpaper. {
ForwardLock.getKey(function(secret) {
ForwardLock.lockBlob(secret, blob, {}, function(lockedBlob) {
activity.postResult({ blob: lockedBlob });
});
});
};
}
}
function displayError(activity, id) {
// using setTimeout here so we don't block with alert() while
// a db transaction is still pending
setTimeout(function() {
alert(_(id));
activity.postError(id);
});
}