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 = 'icons/audio-player-64.png';
15 var AUDIO_PLAYER_APP_URL = 'audio_player.html';
18 * Configuration of the audio player panel.
21 var audioPlayerCreateOptions = {
24 minHeight: 44 + 73, // 44px: track, 73px: controller
26 height: 44 + 73, // collapsed
32 * @extends {BackgroundBase}
34 function AudioPlayerBackground() {
35 BackgroundBase.call(this);
38 AudioPlayerBackground.prototype.__proto__ = BackgroundBase.prototype;
41 * Called when an app is restarted.
43 AudioPlayerBackground.prototype.onRestarted_ = function() {
44 audioPlayer.reopen(function() {
45 // If the audioPlayer is reopened, change its window's icon. Otherwise
46 // there is no reopened window so just skip the call of setIcon.
47 if (audioPlayer.rawAppWindow)
48 audioPlayer.setIcon(AUDIO_PLAYER_ICON);
54 * Backgound object. This is necessary for AppWindowWrapper.
55 * @type {BackgroundBase}
57 var background = new AudioPlayerBackground();
60 * Wrapper of audio player window.
61 * @type {SingletonAppWindowWrapper}
63 var audioPlayer = new SingletonAppWindowWrapper(AUDIO_PLAYER_APP_URL,
64 audioPlayerCreateOptions);
67 * Opens player window.
68 * @param {!Array<string>} urls List of audios to play and index to start
70 * @return {!Promise} Promise to be fulfilled on success, or rejected on error.
74 var startUrl = (position < urls.length) ? urls[position] : '';
76 return new Promise(function(fulfill, reject) {
77 if (urls.length === 0) {
78 reject('No file to open.');
82 // Gets the current list of the children of the parent.
83 window.webkitResolveLocalFileSystemURL(urls[0], function(fileEntry) {
84 fileEntry.getParent(function(parentEntry) {
85 var dirReader = parentEntry.createReader();
88 // Call the reader.readEntries() until no more results are returned.
89 var readEntries = function() {
90 dirReader.readEntries(function(results) {
91 if (!results.length) {
92 fulfill(entries.sort(util.compareName));
94 entries = entries.concat(Array.prototype.slice.call(results, 0));
104 }).then(function(entries) {
105 // Omits non-audio files.
106 var audioEntries = entries.filter(FileType.isAudio);
108 // Adjusts the position to start playing.
109 var maybePosition = util.entriesToURLs(audioEntries).indexOf(startUrl);
110 if (maybePosition !== -1)
111 position = maybePosition;
113 // Opens the audio player panel.
114 return new Promise(function(fulfill, reject) {
115 var urls = util.entriesToURLs(audioEntries);
116 audioPlayer.launch({items: urls, position: position},
118 fulfill.bind(null, null));
121 audioPlayer.setIcon(AUDIO_PLAYER_ICON);
122 audioPlayer.rawAppWindow.focus();
123 return AUDIO_PLAYER_APP_URL;
124 }).catch(function(error) {
125 console.error('Launch failed' + error.stack || error);
126 return Promise.reject(error);
130 background.setLaunchHandler(open);