git-svn-id: https://stereo.googlecode.com/svn/trunk@440 c67ee986-0855-0410-825f-15918...
[stereo.git] / DACPlib / src / util / response / ctrlint / PlayStatusUpdate.java
blob8235372cc069cecf845030a0a429261fdba61e1c
1 package util.response.ctrlint;
3 import interfaces.Constants;
4 import api.Reader;
5 import api.Response;
6 import api.Writer;
8 /**
9 * <playstatusupdate>
10 * <revision>
11 * <state>
12 * <shuffle>
13 * <repeat>
14 * <!-- optional -->
15 * <current>
16 * database id
17 * playlist id
18 * playlist position id
19 * track id
20 * <track title>
21 * <track artist>
22 * <track album>
23 * <track genre>
24 * <album id>
25 * <media kind>
26 * <remaining time>
27 * <total time>
30 public class PlayStatusUpdate extends Response {
32 //required
33 public final int revision;
34 public final Status state;
36 /**
37 * false: no shuffle, true: shuffle
39 public final boolean shuffle;
41 /**
42 * 0: no repeat
43 * 1: repeat song
44 * 2: repeat (playlist)
46 public final int repeat;
48 public PlayStatusUpdate(int revision, boolean shuffle, int repeat) {
49 super(Constants.dmcp_status, Response.OK);
50 this.revision = revision;
51 this.state = Status.STOPPED;
52 this.shuffle = shuffle;
53 this.repeat = repeat;
56 protected PlayStatusUpdate(int revision, Status state, boolean shuffle, int repeat) {
57 super(Constants.dmcp_status, Response.OK);
58 this.revision = revision;
59 this.state = state;
60 this.shuffle = shuffle;
61 this.repeat = repeat;
64 public static PlayStatusUpdate read(Reader reader) {
66 int revision = 0;
67 Status state = Status.STOPPED;
68 boolean shuffle = false;
69 int repeat = 0;
70 int currentDatabase = 0;
71 int currentPlaylist = 0;
72 int currentPlaylistTrack = 0;
73 int currentTrack = 0;
74 String trackTitle = null;
75 String trackArtist = null;
76 String trackAlbum = null;
77 String trackGenre = null;
78 long currentAlbumId = 0;
79 int mediaKind = 0;
80 int remainingTime = 0;
81 int totalTime = 0;
83 for (Constants code: reader) {
85 switch (code) {
86 case dmcp_mediarevision:
87 revision = reader.nextInteger(code);
88 break;
89 case dacp_state:
90 state = status(reader.nextByte(code));
91 break;
92 case dacp_shuffle:
93 shuffle = reader.nextBoolean(code);
94 break;
95 case dacp_repeat:
96 repeat = reader.nextByte(code);
97 break;
98 case dacp_nowplaying:
99 int[] canp = reader.nextLongLong(code);
100 currentDatabase = canp[0];
101 currentPlaylist = canp[1];
102 currentPlaylistTrack = canp[2];
103 currentTrack = canp[3];
104 break;
105 case dacp_nowplayingname:
106 trackTitle = reader.nextString(code);
107 break;
108 case dacp_nowplayingartist:
109 trackArtist = reader.nextString(code);
110 break;
111 case dacp_nowplayingalbum:
112 trackAlbum = reader.nextString(code);
113 break;
114 case dacp_nowplayinggenre:
115 trackGenre = reader.nextString(code);
116 break;
117 case daap_songalbumid:
118 currentAlbumId = reader.nextLong(code);
119 break;
120 case dacp_remainingtime:
121 remainingTime = reader.nextInteger(code);
122 break;
123 case dacp_songtime:
124 totalTime = reader.nextInteger(code);
125 break;
129 if (state == Status.STOPPED) {
130 return new PlayStatusUpdate(revision, shuffle, repeat);
132 else {
133 return new Active(revision, state, shuffle, repeat,
134 currentDatabase, currentPlaylist, currentPlaylistTrack, currentTrack,
135 trackTitle, trackArtist, trackAlbum, trackGenre, currentAlbumId,
136 mediaKind, remainingTime, totalTime);
140 public Constants type() {
141 return Constants.dmcp_status;
144 public void write(Writer writer) {
146 writer.appendInteger(Constants.dmcp_mediarevision, revision);
147 writer.appendByte(Constants.dacp_state, state.value());
148 writer.appendBoolean(Constants.dacp_shuffle, shuffle);
149 writer.appendByte(Constants.dacp_repeat, (byte)repeat);
150 writer.appendInteger(Constants.dacp_albumshuffle, 2);
151 writer.appendInteger(Constants.dacp_albumrepeat, 6);
155 public String toString() {
156 return revision + ": stopped";
159 public static final Status status(byte status) {
160 switch (status) {
161 case 2: return Status.STOPPED;
162 case 3: return Status.PAUSED;
163 case 4: return Status.PLAYING;
165 return null;
168 public Active active() {
169 return null;
172 public enum Status {
173 STOPPED (2),
174 PAUSED (3),
175 PLAYING (4);
177 Status(int value) {
178 this.value = value;
180 public byte value() {
181 return (byte)value;
183 private int value;
187 * <playstatusupdate>
188 * <revision>
189 * <state>
190 * <shuffle>
191 * <repeat>
192 * <current>
193 * database id
194 * playlist id
195 * playlist position id
196 * track id
197 * <track title>
198 * <track artist>
199 * <track album>
200 * <track genre>
201 * <album id>
202 * <media kind>
203 * <remaining time>
204 * <total time>
207 public static class Active extends PlayStatusUpdate {
209 public final int currentDatabase;
210 public final int currentPlaylist;
211 public final int currentPosition;
212 public final int currentTrackId;
214 public final String trackTitle;
215 public final String trackArtist;
216 public final String trackAlbum;
217 public final String trackGenre;
219 public final long currentAlbumId;
220 public final int mediaKind;
221 public final int remainingTime;
222 public final int totalTime;
224 public Active(int revision, Status state, boolean shuffle,
225 int repeat, int currentDatabase, int currentPlaylist,
226 int currentPosition, int currentTrackId, String trackTitle,
227 String trackArtist, String trackAlbum, String trackGenre,
228 long currentAlbumId, int mediaKind, int remainingTime, int totalTime) {
230 super(revision, state, shuffle, repeat);
232 this.currentDatabase = currentDatabase;
233 this.currentPlaylist = currentPlaylist;
234 this.currentPosition = currentPosition;
235 this.currentTrackId = currentTrackId;
236 this.trackTitle = trackTitle;
237 this.trackArtist = trackArtist;
238 this.trackAlbum = trackAlbum;
239 this.trackGenre = trackGenre;
240 this.currentAlbumId = currentAlbumId;
241 this.mediaKind = mediaKind;
242 this.remainingTime = remainingTime;
243 this.totalTime = totalTime;
246 public void write(Writer writer) {
247 super.write(writer);
249 writer.appendLongLong(Constants.dacp_nowplaying, new int[] {
250 currentDatabase,
251 currentPlaylist,
252 currentPosition,
253 currentTrackId
255 writer.appendString(Constants.dacp_nowplayingname, trackTitle);
256 writer.appendString(Constants.dacp_nowplayingartist, trackArtist);
257 writer.appendString(Constants.dacp_nowplayingalbum, trackAlbum);
258 writer.appendString(Constants.dacp_nowplayinggenre, trackGenre);
259 writer.appendLong(Constants.daap_songalbumid, currentAlbumId);
260 writer.appendInteger(Constants.dmcp_mediakind, 1); //media kind - only support songs
261 writer.appendInteger(Constants.dacp_remainingtime, remainingTime);
262 writer.appendInteger(Constants.dacp_songtime, totalTime);
265 public Active active() {
266 return this;
269 public String toString() {
270 return this.revision + ": " + trackTitle + " - " + trackArtist;