OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 'use strict'; |
| 6 |
| 7 /** |
| 8 * Icon of the audio player. |
| 9 * TODO(yoshiki): Consider providing an exact size icon, instead of relying |
| 10 * on downsampling by ash. |
| 11 * |
| 12 * @type {string} |
| 13 * @const |
| 14 */ |
| 15 var AUDIO_PLAYER_ICON = 'audio_player/icons/audio-player-64.png'; |
| 16 |
| 17 // Stores the app windows OLNY for test purpose. |
| 18 // We SHOULD NOT use it as it is except for test, since the files which have |
| 19 // the same name will be overridden each other. |
| 20 var appWindowsForTest = {}; |
| 21 |
| 22 var audioPlayerCreateOptions = { |
| 23 id: 'audio-player', |
| 24 type: 'panel', |
| 25 minHeight: 44 + 73, // 44px: track, 73px: controller |
| 26 minWidth: 292, |
| 27 height: 44 + 73, // collapsed |
| 28 width: 292 |
| 29 }; |
| 30 |
| 31 var audioPlayer = new SingletonAppWindowWrapper('audio_player.html', |
| 32 audioPlayerCreateOptions); |
| 33 |
| 34 var background = new BackgroundBase(); |
| 35 |
| 36 var initializeQueue = new AsyncUtil.Queue(); |
| 37 |
| 38 // Initializes the strings. This needs for the volume manager. |
| 39 initializeQueue.run(function(fulfill) { |
| 40 chrome.fileManagerPrivate.getStrings(function(stringData) { |
| 41 loadTimeData.data = stringData; |
| 42 fulfill(); |
| 43 }); |
| 44 }); |
| 45 |
| 46 // Initializes the volume manager. This needs for isolated entries. |
| 47 initializeQueue.run(function(fulfill) { |
| 48 VolumeManager.getInstance(fulfill); |
| 49 }); |
| 50 |
| 51 // Registers the handlers. |
| 52 chrome.app.runtime.onLaunched.addListener(onLaunched); |
| 53 chrome.app.runtime.onRestarted.addListener(onRestarted); |
| 54 |
| 55 /** |
| 56 * Called when an app is launched. |
| 57 * @param {Object} launchData Launch data. |
| 58 */ |
| 59 function onLaunched(launchData) { |
| 60 if (!launchData || !launchData.items || launchData.items.length == 0) |
| 61 return; |
| 62 |
| 63 var playlist = {}; |
| 64 |
| 65 initializeQueue.run(function(fulfill) { |
| 66 var isolatedEntries = launchData.items.map(function(item) { |
| 67 return item.entry; |
| 68 }); |
| 69 |
| 70 chrome.fileManagerPrivate.resolveIsolatedEntries(isolatedEntries, |
| 71 function(externalEntries) { |
| 72 var urls = util.entriesToURLs(externalEntries); |
| 73 playlist = {items: urls, position: 0}; |
| 74 fulfill(); |
| 75 }); |
| 76 }); |
| 77 |
| 78 initializeQueue.run(function(fulfill) { |
| 79 open(playlist, false); |
| 80 fulfill(); |
| 81 }); |
| 82 } |
| 83 |
| 84 /** |
| 85 * Called when an app is restarted. |
| 86 */ |
| 87 function onRestarted() { |
| 88 audioPlayer.reopen(function() { |
| 89 // If the audioPlayer is reopened, change its window's icon. Otherwise |
| 90 // there is no reopened window so just skip the call of setIcon. |
| 91 if (audioPlayer.rawAppWindow) |
| 92 audioPlayer.setIcon(AUDIO_PLAYER_ICON); |
| 93 }); |
| 94 } |
| 95 |
| 96 /** |
| 97 * Opens player window. |
| 98 * @param {Object} playlist List of audios to play and index to start playing. |
| 99 * @param {Promise} Promise to be fulfilled on success, or rejected on error. |
| 100 */ |
| 101 function open(playlist, reopen) { |
| 102 var items = playlist.items; |
| 103 var position = playlist.position; |
| 104 var startUrl = (position < items.length) ? items[position] : ''; |
| 105 |
| 106 return new Promise(function(fulfill, reject) { |
| 107 if (items.length === 0) { |
| 108 reject('No file to open.'); |
| 109 return; |
| 110 } |
| 111 |
| 112 window.webkitResolveLocalFileSystemURL(items[0], function(fileEntry) { |
| 113 fileEntry.getParent(fulfill, reject); |
| 114 }); |
| 115 }).then(function(parentEntry) { |
| 116 return new Promise(function(fulfill, reject) { |
| 117 var dirReader = parentEntry.createReader(); |
| 118 var entries = []; |
| 119 |
| 120 // Call the reader.readEntries() until no more results are returned. |
| 121 var readEntries = function() { |
| 122 dirReader.readEntries(function(results) { |
| 123 if (!results.length) { |
| 124 fulfill(entries); |
| 125 } else { |
| 126 entries = entries.concat(Array.prototype.slice.call(results, 0)); |
| 127 readEntries(); |
| 128 } |
| 129 }, reject); |
| 130 }; |
| 131 |
| 132 // Start reading. |
| 133 readEntries(); |
| 134 }); |
| 135 }).then(function(entries) { |
| 136 var audioEntries = entries.filter(FileType.isAudio).sort(util.compareName); |
| 137 var maybePosition = util.entriesToURLs(audioEntries).indexOf(startUrl); |
| 138 if (maybePosition !== -1) |
| 139 position = maybePosition; |
| 140 |
| 141 return new Promise(function(fulfill, reject) { |
| 142 var urls = util.entriesToURLs(audioEntries); |
| 143 audioPlayer.launch({items: urls, position: position}, reopen, fulfill); |
| 144 }); |
| 145 }).then(function() { |
| 146 var appWindow = audioPlayer.rawAppWindow; |
| 147 appWindow.setIcon('icons/audio-player-64.png'); |
| 148 AppWindowWrapper.focusOnDesktop(appWindow); |
| 149 }).catch(function(error) { |
| 150 console.error('Launch failed' + error.stack || error); |
| 151 return Promise.reject(error); |
| 152 }); |
| 153 } |
OLD | NEW |