1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * Icon of the audio player.
7 * TODO(yoshiki): Consider providing an exact size icon, instead of relying
8 * on downsampling by ash.
13 var AUDIO_PLAYER_ICON = 'audio_player/icons/audio-player-64.png';
16 * Configuration of the audio player panel.
19 var audioPlayerCreateOptions = {
22 minHeight: 44 + 73, // 44px: track, 73px: controller
24 height: 44 + 73, // collapsed
29 * Backgound object. This is necessary for AppWindowWrapper.
30 * @type {BackgroundBase}
32 var background = new BackgroundBase();
35 * Wrapper of audio player window.
36 * @type {SingletonAppWindowWrapper}
38 var audioPlayer = new SingletonAppWindowWrapper('audio_player.html',
39 audioPlayerCreateOptions);
42 * Queue to serialize initialization.
43 * @type {AsyncUtil.Queue}
45 var initializeQueue = new AsyncUtil.Queue();
47 // Initializes the strings. This needs for the volume manager.
48 initializeQueue.run(function(fulfill) {
49 chrome.fileManagerPrivate.getStrings(function(stringData) {
50 loadTimeData.data = stringData;
55 // Initializes the volume manager. This needs for isolated entries.
56 initializeQueue.run(function(fulfill) {
57 VolumeManager.getInstance(fulfill);
60 // Registers the handlers.
61 chrome.app.runtime.onLaunched.addListener(onLaunched);
62 chrome.app.runtime.onRestarted.addListener(onRestarted);
65 * Called when an app is launched.
66 * @param {Object} launchData Launch data.
68 function onLaunched(launchData) {
69 if (!launchData || !launchData.items || launchData.items.length == 0)
74 initializeQueue.run(function(fulfill) {
75 var isolatedEntries = launchData.items.map(function(item) {
79 chrome.fileManagerPrivate.resolveIsolatedEntries(isolatedEntries,
80 function(externalEntries) {
81 var urls = util.entriesToURLs(externalEntries);
82 playlist = {items: urls, position: 0};
87 initializeQueue.run(function(fulfill) {
88 open(playlist, false);
94 * Called when an app is restarted.
96 function onRestarted() {
97 audioPlayer.reopen(function() {
98 // If the audioPlayer is reopened, change its window's icon. Otherwise
99 // there is no reopened window so just skip the call of setIcon.
100 if (audioPlayer.rawAppWindow)
101 audioPlayer.setIcon(AUDIO_PLAYER_ICON);
106 * Opens player window.
107 * @param {Object} playlist List of audios to play and index to start playing.
108 * @param {boolean} reopen
109 * @return {Promise} Promise to be fulfilled on success, or rejected on error.
111 function open(playlist, reopen) {
112 var items = playlist.items;
113 var position = playlist.position;
114 var startUrl = (position < items.length) ? items[position] : '';
116 return new Promise(function(fulfill, reject) {
117 if (items.length === 0) {
118 reject('No file to open.');
122 // Gets the current list of the children of the parent.
123 window.webkitResolveLocalFileSystemURL(items[0], function(fileEntry) {
124 fileEntry.getParent(function(parentEntry) {
125 var dirReader = parentEntry.createReader();
128 // Call the reader.readEntries() until no more results are returned.
129 var readEntries = function() {
130 dirReader.readEntries(function(results) {
131 if (!results.length) {
132 fulfill(entries.sort(util.compareName));
134 entries = entries.concat(Array.prototype.slice.call(results, 0));
144 }).then(function(entries) {
145 // Omits non-audio files.
146 var audioEntries = entries.filter(FileType.isAudio);
148 // Adjusts the position to start playing.
149 var maybePosition = util.entriesToURLs(audioEntries).indexOf(startUrl);
150 if (maybePosition !== -1)
151 position = maybePosition;
153 // Opens the audio player panel.
154 return new Promise(function(fulfill, reject) {
155 var urls = util.entriesToURLs(audioEntries);
156 audioPlayer.launch({items: urls, position: position},
158 fulfill.bind(null, null));
161 audioPlayer.setIcon('icons/audio-player-64.png');
162 audioPlayer.rawAppWindow.focus();
163 }).catch(function(error) {
164 console.error('Launch failed' + error.stack || error);
165 return Promise.reject(error);
169 // Register the test utils.
170 test.util.registerRemoteTestUtils();