| 1 |
package org.lastpod; |
|---|
| 2 |
|
|---|
| 3 |
import java.util.ArrayList; |
|---|
| 4 |
import java.util.Calendar; |
|---|
| 5 |
import java.util.List; |
|---|
| 6 |
|
|---|
| 7 |
public class MockModel implements Model { |
|---|
| 8 |
private List recentlyPlayed = null; |
|---|
| 9 |
|
|---|
| 10 |
/** |
|---|
| 11 |
* Default constructor. |
|---|
| 12 |
*/ |
|---|
| 13 |
public MockModel() { |
|---|
| 14 |
/* Default constructor. */ |
|---|
| 15 |
} |
|---|
| 16 |
|
|---|
| 17 |
public List getRecentlyPlayed() { |
|---|
| 18 |
return recentlyPlayed; |
|---|
| 19 |
} |
|---|
| 20 |
|
|---|
| 21 |
public void setRecentlyPlayed(List recentlyPlayed) { |
|---|
| 22 |
this.recentlyPlayed = recentlyPlayed; |
|---|
| 23 |
} |
|---|
| 24 |
|
|---|
| 25 |
/** |
|---|
| 26 |
* A utility function to clear the recently played track list. |
|---|
| 27 |
* @param recentlyPlayed |
|---|
| 28 |
*/ |
|---|
| 29 |
public void clearRecentlyPlayed() { |
|---|
| 30 |
recentlyPlayed = new ArrayList(); |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
public void selectAll() { |
|---|
| 34 |
setupSelections(true); |
|---|
| 35 |
} |
|---|
| 36 |
|
|---|
| 37 |
public void unselectAll() { |
|---|
| 38 |
setupSelections(false); |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
private void setupSelections(boolean select) { |
|---|
| 42 |
for (int i = 0; i < recentlyPlayed.size(); i++) { |
|---|
| 43 |
TrackItem trackItem = (TrackItem) recentlyPlayed.get(i); |
|---|
| 44 |
trackItem.setActive(new Boolean(select)); |
|---|
| 45 |
} |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
/** |
|---|
| 49 |
* Parses the play counts and track information from the iPod. |
|---|
| 50 |
*/ |
|---|
| 51 |
public void parsePlayCounts(UI userInterface) { |
|---|
| 52 |
Calendar cal = Calendar.getInstance(); |
|---|
| 53 |
cal.set(Calendar.YEAR, 2007); |
|---|
| 54 |
cal.set(Calendar.MONTH, Calendar.MAY); |
|---|
| 55 |
cal.set(Calendar.DAY_OF_MONTH, 25); |
|---|
| 56 |
|
|---|
| 57 |
for (int i = 0; i <= 30; i++) { |
|---|
| 58 |
TrackItem trackItem = new TrackItem(); |
|---|
| 59 |
trackItem.setTrackid(1); |
|---|
| 60 |
trackItem.setActive(Boolean.TRUE); |
|---|
| 61 |
trackItem.setLength(60); |
|---|
| 62 |
trackItem.setArtist("My Chemical Romance"); |
|---|
| 63 |
trackItem.setAlbum("The Black Parade"); |
|---|
| 64 |
trackItem.setTrack("Welcome To The Black Parade"); |
|---|
| 65 |
trackItem.setPlaycount(1); |
|---|
| 66 |
trackItem.setLastplayed(cal.getTimeInMillis() / 1000); |
|---|
| 67 |
|
|---|
| 68 |
recentlyPlayed.add(trackItem); |
|---|
| 69 |
|
|---|
| 70 |
cal.add(Calendar.HOUR, 1); |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
userInterface.newTrackListAvailable(recentlyPlayed); |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
/** |
|---|
| 77 |
* Submits the tracks to Last.fm |
|---|
| 78 |
* @param userInterface The application's user interface. |
|---|
| 79 |
* @return A status message upon completion. |
|---|
| 80 |
*/ |
|---|
| 81 |
public Object submitTracks(UI userInterface) { |
|---|
| 82 |
List activeRecentPlayed = onlyActiveTrackItems(recentlyPlayed); |
|---|
| 83 |
|
|---|
| 84 |
userInterface.setNumberOfChunks(activeRecentPlayed.size()); |
|---|
| 85 |
|
|---|
| 86 |
for (int i = 0; i < activeRecentPlayed.size(); i++) { |
|---|
| 87 |
try { |
|---|
| 88 |
Thread.sleep(100); |
|---|
| 89 |
} catch (InterruptedException e) { |
|---|
| 90 |
// TODO: handle exception |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
TrackItem trackItem = (TrackItem) activeRecentPlayed.get(i); |
|---|
| 94 |
trackItem.setActive(Boolean.FALSE); |
|---|
| 95 |
userInterface.updateCurrentChunk(i + 1); |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
userInterface.setCompletionStatus(true); |
|---|
| 99 |
|
|---|
| 100 |
/* Refresh track list. */ |
|---|
| 101 |
userInterface.newTrackListAvailable(recentlyPlayed); |
|---|
| 102 |
|
|---|
| 103 |
return "Success"; |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
private List onlyActiveTrackItems(List recentPlayed) { |
|---|
| 107 |
return filterTrackItems(recentPlayed, true); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
private List filterTrackItems(List recentPlayed, boolean filterActive) { |
|---|
| 111 |
List filteredRecentPlayed = new ArrayList(); |
|---|
| 112 |
|
|---|
| 113 |
for (int i = 0; i < recentPlayed.size(); i++) { |
|---|
| 114 |
TrackItem trackItem = (TrackItem) recentPlayed.get(i); |
|---|
| 115 |
|
|---|
| 116 |
boolean trackActive = trackItem.isActive().booleanValue(); |
|---|
| 117 |
|
|---|
| 118 |
if (trackActive && filterActive) { |
|---|
| 119 |
filteredRecentPlayed.add(trackItem); |
|---|
| 120 |
} else if (!trackActive && !filterActive) { |
|---|
| 121 |
filteredRecentPlayed.add(trackItem); |
|---|
| 122 |
} |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
return filteredRecentPlayed; |
|---|
| 126 |
} |
|---|
| 127 |
} |
|---|