Project for Continous Integration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
7.0 KiB

package device.usbPlayer;
import device.Device;
import java.lang.reflect.Method;
import java.util.ArrayList;
public class UsbPlayer implements Device {
private ArrayList<String> Playlist = new ArrayList<String>();
private String playTrack = "";
private Boolean isConnected;
private Boolean isDefect;
int Lautstaerke = 0;
int soundWas = -1;
public USB_Stick USBDrive = new USB_Stick();
public ArrayList<String> getPlaylist() {
return Playlist;
}
public void setPlaylist(ArrayList<String> playlist) {
Playlist = playlist;
}
public Boolean getDefect() {
return isDefect;
}
public void setDefect(Boolean defect) {
isDefect = defect;
}
public Boolean getConnected() {
return isConnected;
}
public void setConnected(Boolean connected) {
isConnected = connected;
}
public String getPlayTrack() {
return playTrack;
}
public void setPlayTrack(String playTrack) {
if (this.findSong(playTrack) != -1) {
this.playTrack = playTrack;
} else System.out.println("The Song does not exist in the Albume");
}
public void setLautstaerke(int ls) {
if (ls <= 100) {
Lautstaerke = ls;
}
}
public int getLautstaerke() {
return Lautstaerke;
}
public UsbPlayer() {}
//*********constructure********
public UsbPlayer(USB_Stick stick) {
super();
isDefect = false;
isConnected = true;
try {
Playlist = stick.getAlbum();
} catch (NoFolderFound e) {
e.printStackTrace();
}
if (isAllAudio()) {
playTrack = Playlist.get(0);
}
}
//**********search method*************
public boolean isAllAudio() {
String song = "";
boolean flag = true;
for (int i = 0; i < Playlist.size(); i++) {
song = Playlist.get(i).substring(Playlist.get(i).length() - 4);
if (song.equals(".mp3") | song.equals(".mp4") | song.equals(".m4a") | song.equals(".wav") | song.equals(".wma") | song.equals(".aac")) {
flag = true;
} else {
flag = false;
break;
}
}
return flag;
}
//**********search method*************
@Override
public void quieter() {
if (this.soundWas == -1) {
if (Lautstaerke > 0) {
Lautstaerke -= 1;
} else {
Lautstaerke = 0;
}
}
}
@Override
public void louder() {
if (this.soundWas == -1) {
if (Lautstaerke < 100) {
Lautstaerke += 1;
} else {
Lautstaerke = 100;
}
}
}
public void Louder_10() {
if (this.soundWas == -1) {
Lautstaerke += 10;
if (Lautstaerke > 100) {
Lautstaerke = 100;
}
}
}
public void quieter_10() {
if (this.soundWas == -1) {
Lautstaerke -= 10;
if (Lautstaerke < 0) {
Lautstaerke = 0;
}
}
}
@Override
public int getVolume() {
return Lautstaerke;
}
@Override
public void next() {
int currentIdex = Playlist.indexOf(playTrack);
int nextIndex = (currentIdex + 1) % Playlist.size();
playTrack = Playlist.get(nextIndex);
}
@Override
public void prev() {
int currentIdex = Playlist.indexOf(playTrack);
int nextIndex = Playlist.size() - 1;
if (currentIdex != 0) {
nextIndex = (currentIdex - 1);
}
playTrack = Playlist.get(nextIndex);
}
@Override
public String getInfoText() {
String InfoText = "";
if (isAllAudio()) {
InfoText = "USB Player is on : " + this.getPlayTrack();
} else
InfoText = "USB Player with no Albums";
return InfoText;
}
@Override
public String[] getOptions() {
Method[] methods=this.getClass().getDeclaredMethods();
String[] methodsList =new String[methods.length];
for (int i = 0; i <methods.length ; i++) {
methodsList[i]=methods[i].getName();
}
return methodsList;
}
@Override
public String chooseItem(int itemNr) {
String Message="";
if (itemNr < this.getPlaylist().size() && itemNr >= 0) {
Message= this.getPlaylist().get(itemNr ).toString();
}else if(itemNr < 0){
Message="The Nr is smaller than 0";
}else Message="The Nr is bigger than 0";
return Message;
}
@Override
public String[] getItemList() {
String array[] = new String[getPlaylist().size()];
for(int j =0;j<getPlaylist().size();j++){
array[j] = getPlaylist().get(j);
}
return array;
}
@Override
public String mute() {
this.soundWas = getLautstaerke();
setLautstaerke(0);
return "muted";
}
@Override
public String unmute() {
setLautstaerke(soundWas);
this.soundWas = -1;
return "unmuted";
}
@Override
public String play() {
String result = "USB : ";
if (isConnected == true) {
result += "connected,";
if (isDefect == false) {
result += "working";
if (this.Playlist.size() != 0) {
result += ",have a list";
if (this.isAllAudio()) {
result += ",Audio";
if (!playTrack.equals(""))
result += ",point to a track.";
else
result += ",doesn't point to a track.";
} else {
result += ",not Audio.";
}
} else result += ",have no list.";
} else {
result += "not working.";
}
} else result = "not connected.";
return result;
}
public int findSong(String songName) {
int songIndex = -1, index = -1;
String song;
for (int j = 0; j < getPlaylist().size(); j++) {
song = getPlaylist().get(j);
songIndex = song.toLowerCase().indexOf(songName.toLowerCase());
if (songIndex != -1) {
index = j;
break;
}
}
return index;
}
public boolean GoTo(String songName) {
if (findSong(songName) != -1) {
playTrack = getPlaylist().get(findSong(songName));//songName;
return true;
} else
//playTrack = songName;
return false;
}
public void GoToFirst() {
//go to the first song in the list
setPlayTrack(getPlaylist().get(0));
}
public void GoToLast() {
//go to the last Song
setPlayTrack(this.getPlaylist().get(this.getPlaylist().size() - 1));
}
}