View Javadoc

1   /***
2    *     Ambient - A music player for the Android platform
3    Copyright (C) 2007 Martin Vysny
4    
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9    
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14  
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  package sk.baka.ambient.activity.main;
19  
20  import java.util.List;
21  
22  import sk.baka.ambient.AppState;
23  import sk.baka.ambient.IPlaylistPlayerListener;
24  import sk.baka.ambient.PlaylistPlayer;
25  import sk.baka.ambient.R;
26  import sk.baka.ambient.collection.TrackMetadataBean;
27  import sk.baka.ambient.commons.Interval;
28  import sk.baka.ambient.playerservice.PlayerStateEnum;
29  import sk.baka.ambient.playlist.PlaylistItem;
30  import sk.baka.ambient.playlist.Random;
31  import sk.baka.ambient.playlist.Repeat;
32  
33  /***
34   * Manages the playlist.
35   * 
36   * @author Martin Vysny
37   */
38  public final class PlaylistController extends AbstractPlaylistController
39  		implements IPlaylistPlayerListener {
40  
41  	/***
42  	 * The playlist view id.
43  	 */
44  	public static final int PLAYLIST_VIEW_ID = android.R.id.list;
45  
46  	/***
47  	 * The playlist reference.
48  	 */
49  	private final PlaylistPlayer playlist = app.getPlaylist();
50  
51  	/***
52  	 * Currently playing track.
53  	 */
54  	private PlaylistItem currentTrack = null;
55  
56  	@Override
57  	protected boolean isCurrentlyPlayedTrack(int index) {
58  		return playlist.getPlayItems().get(index) == currentTrack;
59  	}
60  
61  	/***
62  	 * Creates the controller
63  	 * 
64  	 * @param mainActivity
65  	 *            the activity
66  	 */
67  	public PlaylistController(final MainActivity mainActivity) {
68  		super(R.id.playlist, PLAYLIST_VIEW_ID, mainActivity);
69  		currentTrack = playlist.getCurrentlyPlayingItem();
70  		initialize();
71  	}
72  
73  	@Override
74  	protected TrackMetadataBean getTrack(int index) {
75  		return playlist.getPlayItems().get(index).getTrack();
76  	}
77  
78  	public void itemActivated(final int index, final Object model) {
79  		playlist.play(index);
80  	}
81  
82  	@Override
83  	public void removeItems(Interval remove) {
84  		playlist.remove(remove);
85  		super.removeItems(remove);
86  	}
87  
88  	@Override
89  	protected int getPlaylistSize() {
90  		return playlist.size();
91  	}
92  
93  	@Override
94  	public void dropItems(List<TrackMetadataBean> tracks, int x, int y,
95  			int index) {
96  		playlist.add(index, tracks);
97  	}
98  
99  	@Override
100 	public Interval moveItems(Interval highlight, int index) {
101 		final Interval result = playlist.move(highlight, index);
102 		// don't wait for update() to be called asynchronously - if user pushes
103 		// buttons too quickly the selection changes its state because of
104 		// desynchronized move and update operations
105 		update(result);
106 		return result;
107 	}
108 
109 	@Override
110 	public Interval moveItemsByOne(Interval highlight, boolean down) {
111 		final Interval result = playlist.moveBy(highlight, down ? 1 : -1);
112 		// don't wait for update() to be called asynchronously - if user pushes
113 		// buttons too quickly the selection changes its state because of
114 		// desynchronized move and update operations
115 		update(result);
116 		return result;
117 	}
118 
119 	public void trackChanged(final PlaylistItem track, final boolean play,
120 			final int positionMillis) {
121 		currentTrack = track;
122 		listView.getModel().notifyModified();
123 	}
124 
125 	public void playbackStateChanged(PlayerStateEnum state) {
126 		// do nothing
127 	}
128 
129 	public void randomChanged(Random random) {
130 		// do nothing
131 	}
132 
133 	public void repeatChanged(Repeat repeat) {
134 		// do nothing
135 	}
136 
137 	public void trackPositionChanged(final int position, final boolean playing) {
138 		// do nothing
139 	}
140 
141 	public void playlistChanged(final Interval target) {
142 		update(target);
143 	}
144 
145 	@Override
146 	public String getHint(Interval highlight) {
147 		return listView.getResources().getString(R.string.numTracks,
148 				highlight.length);
149 	}
150 
151 	@Override
152 	protected int getQueueNumber(int index) {
153 		if (index >= playlist.size())
154 			return 0;
155 		return playlist.getPlayItems().get(index).getQueueOrder();
156 	}
157 
158 	public void stateChanged(AppState state) {
159 		// do nothing
160 	}
161 }