fixed patch trailing CR
[webos-internals:shivaramv-modifications.git] / musicplayer / bookmark.patch
1 Patch the musicplayer application to record a bookmark when pausing, changing tracks, or exiting the app.
2         
3 You can return to the bookmark by hitting "next" while playing a song, assuming you are before the bookmark.
4         
5 Bookmarks will be displayed in the playlist screen as a red line.
6         
7 Tested-On: 1.1
8
9 see: http://predev.wikidot.com/bookmarking-mediaplayer for more info
10 Index: /usr/palm/applications/com.palm.app.musicplayer/app/controllers/localnowplaying-assistant.js
11 ===================================================================
12 --- .orig/usr/palm/applications/com.palm.app.musicplayer/app/controllers/localnowplaying-assistant.js
13 +++ /usr/palm/applications/com.palm.app.musicplayer/app/controllers/localnowplaying-assistant.js
14 @@ -360,8 +360,12 @@ var LocalnowplayingAssistant = Class.cre
15                                         this._updateXOfYHeader();
16                                 }.bind(this));                          
17                         }  else if (message == this.musicPlayer.MESSAGE_PAUSE) {
18 +                               // drnull - update the bookmark line when pausing for feedback
19 +                               this._updateBookmarkLine();
20                                 this.showPlay();
21                         }  else if (message == this.musicPlayer.MESSAGE_PLAY) {
22 +                               // drnull - update the bookmark line when pausing for feedback
23 +                               this._updateBookmarkLine();
24                                 this.showPause();
25                         } else if (message == this.musicPlayer.MESSAGE_PLAYLIST_ENDED) {
26                                 this.showPlay();                                
27 @@ -986,6 +990,9 @@ var LocalnowplayingAssistant = Class.cre
28                                         this.nextSong();
29                                 } else if (event.command == "prev"){
30                                         this.prevSong();                                                                                        
31 +                                       // drnull - a little bit hacky, but if you hit prev, it could 
32 +                                       // create a new bookmark where you were, so update here
33 +                                       this._updateBookmarkLine();
34                                 } else if (event.command == "pause"){
35                                         this.showPlay();
36                                         this.pause();
37 @@ -1395,6 +1402,9 @@ var LocalnowplayingAssistant = Class.cre
38                                                 //oldSongDiv.setAttribute ("class", oldSongItem.playingStatus); // done by markitemsPlayed
39                                                 oldSongDiv.down('.elapsed').innerHTML = "00:00";
40                                                 oldSongDiv.down('.progress-bar').style.width = 0;
41 +                                               // drnull
42 +                                               // make the line for non-active songs thinner
43 +                                               if (oldSongDiv.down('.bookmark-line').style.width == "2px") oldSongDiv.down('.bookmark-line').style.width = "1px";
44                                         }
45                                 }
46  
47 @@ -1568,6 +1578,24 @@ var LocalnowplayingAssistant = Class.cre
48                 },
49                 
50                 /**
51 +                * drnull - added to support the bookmark line
52 +                * Fetch the bookmark line div.
53 +                */
54 +               _getCurrentSongBookmarkLineDiv: function(){
55 +                       var result = null;
56 +
57 +                       if (this.controller.get('song_list') && this.displayedSong) {
58 +                               var bookmarkDiv = this.controller.get('song_list').select('[x-mojo-media-songid="' + this.displayedSong.songID + '"]')[0];
59 +                               if (bookmarkDiv) {
60 +                                       result = bookmarkDiv.down('.bookmark-line');
61 +                               }
62 +                       }
63 +
64 +                       return result;
65 +               },
66 +
67 +               
68 +               /**
69                  * Format the duration into a displayable string.
70                  * 
71                  * @param {Object} secs
72 @@ -1657,6 +1685,30 @@ var LocalnowplayingAssistant = Class.cre
73                 _updateProgressBar: function(percentage){
74                         this._getCurrentSongProgressBarDiv().style.width =  percentage + "%";
75                 },
76 +
77 +               /**
78 +                * drnull - added to support the bookmark line
79 +                */
80 +               _updateBookmarkLine: function(){
81 +                       var div=this._getCurrentSongBookmarkLineDiv();
82 +                       if (div) {
83 +                               if (this.musicPlayer.hasBookmark()) {
84 +                                       var secs = 0;
85 +                                       if (this.musicPlayer.audioPlayer.duration){
86 +                                               secs = this.musicPlayer.audioPlayer.duration;
87 +                                       } else if (this.musicPlayer.getCurrentSong().songDuration) {
88 +                                               secs = this.musicPlayer.getCurrentSong().songDuration;
89 +                                       }
90 +                                       var left = Math.max(0, 100*this.musicPlayer.getBookmarkTime()/secs - 1);
91 +                                       div.style.left = left + "%";
92 +                                       div.style.width = "2px";
93 +                               }
94 +                       } else {
95 +                               Mojo.Log.error("couldn't find bookmark div, trying again in a bit...");
96 +                               // call back later if we couldn't find the div
97 +                               setTimeout(this._updateBookmarkLine.bind(this), 500);
98 +                       }
99 +               },
100                 
101                 refreshAppMenu: function(){
102                         var menuModel = this._appMenuModel;
103 Index: /usr/palm/applications/com.palm.app.musicplayer/app/views/localnowplaying/listitem.html
104 ===================================================================
105 --- .orig/usr/palm/applications/com.palm.app.musicplayer/app/views/localnowplaying/listitem.html
106 +++ /usr/palm/applications/com.palm.app.musicplayer/app/views/localnowplaying/listitem.html
107 @@ -3,6 +3,7 @@
108                 <div class="#{playingStatus}" x-mojo-media-songid="#{songID}">
109                 
110                         <div class="progress-bar" style="width: #{songCompletedPercentage}%"></div>
111 +                       <div class="bookmark-line" style="width: #{bookmarkWidth}; left: #{bookmarkPercentage}%"></div>
112                         <div class="progress-bar-background"></div>
113  
114                         <div class="now-playing-list title truncating-text">#{songName}</div>
115 Index: /usr/palm/applications/com.palm.app.musicplayer/scripts/MusicPlayer.js
116 ===================================================================
117 --- .orig/usr/palm/applications/com.palm.app.musicplayer/scripts/MusicPlayer.js
118 +++ /usr/palm/applications/com.palm.app.musicplayer/scripts/MusicPlayer.js
119 @@ -49,6 +49,9 @@ var musicPlayerSingleton = (function(){
120                 },
121                 
122                 stop: function(){
123 +                       // drnull
124 +                       // save current position before leaving
125 +                       this.setBookmark();
126                         if (!this.stopped){
127                         }
128                         
129 @@ -188,7 +191,8 @@ var musicPlayerSingleton = (function(){
130          },
131          
132          pause: function(){
133 -                       
134 +                       // drnull
135 +                       this.setBookmark();
136                         if (this.audioPlayer.networkState != HTMLMediaElement.LOADING)
137                         this.audioPlayer.pause();
138                         else
139 @@ -363,6 +367,14 @@ var musicPlayerSingleton = (function(){
140                         Mojo.Log.info ("current song: " + currentSongNum)
141                         Mojo.Log.info ("num songs: " + this.getNumSongs())                      
142                 
143 +                       // drnull
144 +                       // if we hit "next" but we are not up to the bookmarked time yet, go ahead and jump to it
145 +                       if (this.hasBookmark() && this.audioPlayer.currentTime+1 < this.getBookmarkTime()) {
146 +                               this.gotoBookmark();
147 +                       } else {
148 +                               //drnull
149 +                               //save current position before jumping to new song
150 +                               this.setBookmark();
151                         if (currentSongNum == this.getNumSongs() - 1 &&
152                                 this.getRepeatMode() != this.playlist.REPEAT_MODE_ALL)  {
153                                 // if the playlist is over
154 @@ -376,6 +388,7 @@ var musicPlayerSingleton = (function(){
155                                 else
156                                         this.app.handleNextSong(playlistEnded, doNotSwitchSongs, this._doNextSong.bind(this));
157                         }
158 +                       }
159                 
160                                 
161                         return true;                           
162 @@ -403,6 +416,9 @@ var musicPlayerSingleton = (function(){
163                         
164                         if ((this.audioPlayer.currentTime > 4  && !force) || 
165                                 (currentSongNum == 0 && this.getRepeatMode() != this.playlist.REPEAT_MODE_ALL)) {
166 +                               // drnull
167 +                               // if we are going back to the beginning, set a bookmark first
168 +                               this.setBookmark();
169                  this.audioPlayer.currentTime = 0;
170              }
171                         else if (currentSongNum == 0 &&
172 @@ -479,6 +495,10 @@ var musicPlayerSingleton = (function(){
173                 },
174                 
175          jumpToSong: function(songNum, callback, doNotStartPlaying){
176 +                       // drnull
177 +                       // save current position before jumping to new song
178 +                       this.setBookmark();
179 +
180          
181              this.playlist.setSong(songNum, function(){
182                  this._openCurrentSong(this.isPlaying, doNotStartPlaying);
183 @@ -489,6 +509,99 @@ var musicPlayerSingleton = (function(){
184              
185          },
186          
187 +               /**
188 +                * drnull
189 +                * Save the current play position in the song
190 +                */
191 +               setBookmark: function() {
192 +                       var cookie = this._bookmarkCookie();
193 +                       if (cookie && this.audioPlayer.currentTime > 15) {
194 +                               Mojo.Log.error("setBookmark:", this.playlist.getCurrentSong().songID, "-", this.playlist.getCurrentSong().songName, ": bookmark=", this.audioPlayer.currentTime);
195 +                               cookie.put(this.audioPlayer.currentTime + " " + Math.max(0, 100*this.audioPlayer.currentTime/this.audioPlayer.duration-1));
196 +                       }
197 +               },
198 +
199 +               /**
200 +                * drnull
201 +                * goto the bookmark stored in a cookie if available
202 +                */
203 +               gotoBookmark: function() {
204 +                       var bookmarkTime = this.getBookmarkTime();
205 +                       if (bookmarkTime > 0) {
206 +                               if (this.audioPlayer.duration < bookmarkTime) {
207 +                                       this.audioPlayer.currentTime = this.audioPlayer.duration;
208 +                               } else {
209 +                                       this.audioPlayer.currentTime = bookmarkTime;
210 +                               }
211 +
212 +                               Mojo.Log.error("gotoBookmark:", this.playlist.getCurrentSong().songID, "-", this.playlist.getCurrentSong().songName, ": bookmark=", bookmarkTime);
213 +                       }
214 +               },
215 +
216 +               /**
217 +                * drnull
218 +                * find out if this song has a bookmark cookie
219 +                */
220 +               hasBookmark: function(id) {
221 +                       var hasCookie = false;
222 +                       var cookie = this._bookmarkCookie(id);
223 +                       if (cookie) {
224 +                               if (cookie.get() != null) hasCookie = true;
225 +                       }
226 +                       return hasCookie;
227 +               },
228 +
229 +               /**
230 +                * drnull
231 +                * get the bookmark time index for this song
232 +                */
233 +               getBookmarkTime: function(id) {
234 +                       var cookie = this._bookmarkCookie(id);
235 +                       var index = 0;
236 +                       if (cookie) {
237 +                               value = cookie.get();
238 +                               if (value) {
239 +                                       arr = new String(value).split(" ");
240 +                                       index = arr[0];
241 +                                       if (index == null) index = 0;
242 +                               }
243 +                       }
244 +                       return index;
245 +               },
246 +
247 +               /**
248 +                * drnull
249 +                * get the bookmark percent for this song
250 +                */
251 +               getBookmarkPercentage: function(id) {
252 +                       var cookie = this._bookmarkCookie(id);
253 +                       var index = 0;
254 +                       if (cookie) {
255 +                               value = cookie.get();
256 +                               if (value) {
257 +                                       arr = new String(value).split(" ");
258 +                                       index = arr[1];
259 +                                       if (index == null) index = 0;
260 +                               }
261 +                       }
262 +                       return index;
263 +               },
264 +
265 +                       /**
266 +                        * drnull
267 +                        * single point of access to the cookie
268 +                        */
269 +                       _bookmarkCookie: function(id) {
270 +                               var cookie = null;
271 +                               var songID = id;
272 +                               if (songID == null && this.playlist && this.playlist.getCurrentSong())
273 +                                       songID = this.playlist.getCurrentSong().songID;
274 +                               if (songID != null) {
275 +                                       cookie = new Mojo.Model.Cookie("com.palm.app.musicplayer.bookmark.song"+songID);
276 +                               }
277 +                               return cookie;
278 +                       },
279 +
280          /**
281           * Toggle the reor mode on/off.
282           */
283 Index: /usr/palm/applications/com.palm.app.musicplayer/scripts/ReorderablePlaylist.js
284 ===================================================================
285 --- .orig/usr/palm/applications/com.palm.app.musicplayer/scripts/ReorderablePlaylist.js
286 +++ /usr/palm/applications/com.palm.app.musicplayer/scripts/ReorderablePlaylist.js
287 @@ -116,6 +116,14 @@ var ReorderablePlaylist = Class.create({
288                 
289                 song.playingStatus = this._playingStatus (songNum);
290                 song.songNum = songNum;
291 +               //drnull
292 +               if (this.musicPlayer.hasBookmark(song.songID)) {
293 +                       song.bookmarkPercentage = this.musicPlayer.getBookmarkPercentage(song.songID);
294 +                       song.bookmarkWidth = "1px";
295 +               } else {
296 +                       song.bookmarkPercentage = "0";
297 +                       song.bookmarkWidth = "0px";
298 +               }
299                 
300                 if (songNum == this.getCurrentSongNum()){
301                         var currentTime = Math.floor(this.musicPlayer.audioPlayer.currentTime);
302 Index: /usr/palm/applications/com.palm.app.musicplayer/stylesheets/music-player.css
303 ===================================================================
304 --- .orig/usr/palm/applications/com.palm.app.musicplayer/stylesheets/music-player.css
305 +++ /usr/palm/applications/com.palm.app.musicplayer/stylesheets/music-player.css
306 @@ -741,6 +741,9 @@ img.album-art {
307  .palm-reorder-element .active .progress-bar {
308         display: none;
309  }
310 +.palm-reorder-element .bookmark-line {
311 +       display: none;
312 +}
313  
314  .active .progress-bar-background {
315         position: absolute;
316 @@ -749,6 +752,16 @@ img.album-art {
317         border-width: 15px 15px 11px 15px;
318         -webkit-border-image: url(../images/download-background.png) 15 15 11 15 stretch stretch;       
319         -webkit-box-sizing: border-box;
320 +       z-index: -3;
321 +}
322 +
323 +.bookmark-line {
324 +       position: absolute;
325 +       height: 58px; 
326 +       left: 0%;
327 +       width: 0px;
328 +       color: red;
329 +       background-color: red;
330         z-index: -2;
331  }
332