2013-02-08 10:09:55 +01:00
|
|
|
/*
|
|
|
|
This file is part of Subsonic.
|
|
|
|
|
|
|
|
Subsonic is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Subsonic is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright 2009 (C) Sindre Mehus
|
|
|
|
*/
|
2013-05-16 09:59:55 +02:00
|
|
|
package com.thejoshwa.ultrasonic.androidapp.view;
|
2013-02-08 10:09:55 +01:00
|
|
|
|
|
|
|
import android.content.Context;
|
2013-04-28 01:01:34 +02:00
|
|
|
import android.graphics.drawable.Drawable;
|
2013-02-08 10:09:55 +01:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Checkable;
|
|
|
|
import android.widget.CheckedTextView;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
2013-04-06 21:47:24 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.R;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.MusicDirectory;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.service.DownloadService;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.service.DownloadServiceImpl;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.service.DownloadFile;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.service.MusicService;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.service.MusicServiceFactory;
|
2013-05-16 09:59:55 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.Util;
|
2013-05-31 10:02:25 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.VideoPlayerType;
|
2013-02-08 10:09:55 +01:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to display songs in a {@code ListView}.
|
|
|
|
*
|
|
|
|
* @author Sindre Mehus
|
|
|
|
*/
|
2013-05-16 09:59:55 +02:00
|
|
|
public class SongView extends UpdateView implements Checkable {
|
2013-02-08 10:09:55 +01:00
|
|
|
|
|
|
|
private static final String TAG = SongView.class.getSimpleName();
|
|
|
|
private CheckedTextView checkedTextView;
|
|
|
|
private ImageView starImageView;
|
2013-04-07 04:14:26 +02:00
|
|
|
private TextView trackTextView;
|
2013-02-08 10:09:55 +01:00
|
|
|
private TextView titleTextView;
|
|
|
|
private TextView artistTextView;
|
|
|
|
private TextView durationTextView;
|
|
|
|
private TextView statusTextView;
|
|
|
|
private MusicDirectory.Entry song;
|
2013-05-16 09:59:55 +02:00
|
|
|
|
|
|
|
private DownloadService downloadService;
|
|
|
|
|
|
|
|
public SongView(Context context) {
|
2013-02-08 10:09:55 +01:00
|
|
|
super(context);
|
|
|
|
LayoutInflater.from(context).inflate(R.layout.song_list_item, this, true);
|
|
|
|
|
|
|
|
checkedTextView = (CheckedTextView) findViewById(R.id.song_check);
|
|
|
|
starImageView = (ImageView) findViewById(R.id.song_star);
|
2013-04-07 04:14:26 +02:00
|
|
|
trackTextView = (TextView) findViewById(R.id.song_track);
|
2013-02-08 10:09:55 +01:00
|
|
|
titleTextView = (TextView) findViewById(R.id.song_title);
|
|
|
|
artistTextView = (TextView) findViewById(R.id.song_artist);
|
|
|
|
durationTextView = (TextView) findViewById(R.id.song_duration);
|
|
|
|
statusTextView = (TextView) findViewById(R.id.song_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSong(final MusicDirectory.Entry song, boolean checkable) {
|
|
|
|
this.song = song;
|
2013-04-07 04:14:26 +02:00
|
|
|
StringBuilder artist = new StringBuilder(60);
|
2013-02-08 10:09:55 +01:00
|
|
|
|
|
|
|
String bitRate = null;
|
|
|
|
if (song.getBitRate() != null) {
|
|
|
|
bitRate = String.format(getContext().getString(R.string.song_details_kbps), song.getBitRate());
|
|
|
|
}
|
|
|
|
|
2013-05-31 10:02:25 +02:00
|
|
|
VideoPlayerType videoPlayer = Util.getVideoPlayerType(getContext());
|
|
|
|
|
2013-02-08 10:09:55 +01:00
|
|
|
String fileFormat = null;
|
2013-05-31 10:02:25 +02:00
|
|
|
if (song.getTranscodedSuffix() == null || song.getTranscodedSuffix().equals(song.getSuffix()) || (song.isVideo() && videoPlayer != VideoPlayerType.FLASH)) {
|
|
|
|
fileFormat = song.getSuffix();
|
|
|
|
} else {
|
2013-02-08 10:09:55 +01:00
|
|
|
fileFormat = String.format("%s > %s", song.getSuffix(), song.getTranscodedSuffix());
|
2013-05-31 10:02:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String artistName = song.getArtist();
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-05-31 10:02:25 +02:00
|
|
|
if (artistName != null) {
|
|
|
|
if (Util.shouldDisplayBitrateWithArtist(getContext())) {
|
|
|
|
artist.append(artistName).append(" (")
|
|
|
|
.append(String.format(getContext().getString(R.string.song_details_all), bitRate == null ? "" : bitRate + " ", fileFormat))
|
|
|
|
.append(")");
|
|
|
|
} else {
|
|
|
|
artist.append(artistName);
|
|
|
|
}
|
|
|
|
}
|
2013-04-07 04:14:26 +02:00
|
|
|
|
|
|
|
int trackNumber = song.getTrack();
|
|
|
|
|
2013-04-09 08:01:45 +02:00
|
|
|
if (Util.shouldShowTrackNumber(getContext()) && trackNumber != 0) {
|
2013-04-07 04:14:26 +02:00
|
|
|
trackTextView.setText(String.format("%02d.", trackNumber));
|
|
|
|
} else {
|
|
|
|
trackTextView.setVisibility(View.GONE);
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
|
|
|
titleTextView.setText(song.getTitle());
|
|
|
|
artistTextView.setText(artist);
|
2013-05-31 10:02:25 +02:00
|
|
|
durationTextView.setText(Util.formatTotalDuration(song.getDuration()));
|
2013-04-28 01:01:34 +02:00
|
|
|
starImageView.setImageDrawable(song.getStarred() ? Util.getDrawableFromAttribute(getContext(), R.attr.star_full) : Util.getDrawableFromAttribute(getContext(), R.attr.star_hollow));
|
2013-02-08 10:09:55 +01:00
|
|
|
checkedTextView.setVisibility(checkable && !song.isVideo() ? View.VISIBLE : View.GONE);
|
|
|
|
|
2013-04-07 04:14:26 +02:00
|
|
|
if (Util.isOffline(getContext())) {
|
|
|
|
starImageView.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
2013-02-08 10:09:55 +01:00
|
|
|
starImageView.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
final boolean isStarred = song.getStarred();
|
|
|
|
final String id = song.getId();
|
|
|
|
|
|
|
|
if (!isStarred) {
|
2013-04-28 01:01:34 +02:00
|
|
|
starImageView.setImageDrawable(Util.getDrawableFromAttribute(getContext(), R.attr.star_full));
|
2013-02-08 10:09:55 +01:00
|
|
|
song.setStarred(true);
|
|
|
|
} else {
|
2013-04-28 01:01:34 +02:00
|
|
|
starImageView.setImageDrawable(Util.getDrawableFromAttribute(getContext(), R.attr.star_hollow));
|
2013-02-08 10:09:55 +01:00
|
|
|
song.setStarred(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
MusicService musicService = MusicServiceFactory.getMusicService(null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!isStarred) {
|
2013-05-18 12:50:54 +02:00
|
|
|
musicService.star(id, null, null, getContext(), null);
|
2013-02-08 10:09:55 +01:00
|
|
|
} else {
|
2013-05-18 12:50:54 +02:00
|
|
|
musicService.unstar(id, null, null, getContext(), null);
|
2013-02-08 10:09:55 +01:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(TAG, e.getMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
updateBackground();
|
2013-02-08 10:09:55 +01:00
|
|
|
update();
|
|
|
|
}
|
2013-05-16 09:59:55 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void updateBackground() {
|
|
|
|
if (downloadService == null) {
|
|
|
|
|
|
|
|
downloadService = DownloadServiceImpl.getInstance();
|
|
|
|
|
|
|
|
if(downloadService == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadService.forSong(song);
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
@Override
|
|
|
|
protected void update() {
|
2013-02-08 10:09:55 +01:00
|
|
|
if (downloadService == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DownloadFile downloadFile = downloadService.forSong(song);
|
2013-05-16 09:59:55 +02:00
|
|
|
downloadFile.getCompleteFile();
|
2013-02-08 10:09:55 +01:00
|
|
|
File partialFile = downloadFile.getPartialFile();
|
|
|
|
|
2013-04-28 01:01:34 +02:00
|
|
|
Drawable leftImage = null;
|
|
|
|
Drawable rightImage = null;
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
if (downloadFile.isWorkDone()) {
|
2013-05-02 08:53:33 +02:00
|
|
|
leftImage = downloadFile.isSaved() ? Util.getDrawableFromAttribute(getContext(), R.attr.unpin) : Util.getDrawableFromAttribute(getContext(), R.attr.downloaded);
|
2013-02-08 10:09:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (downloadFile.isDownloading() && !downloadFile.isDownloadCancelled() && partialFile.exists()) {
|
|
|
|
statusTextView.setText(Util.formatLocalizedBytes(partialFile.length(), getContext()));
|
2013-04-28 01:01:34 +02:00
|
|
|
rightImage = Util.getDrawableFromAttribute(getContext(), R.attr.downloading);
|
2013-02-08 10:09:55 +01:00
|
|
|
} else {
|
|
|
|
statusTextView.setText(null);
|
|
|
|
}
|
2013-04-28 01:01:34 +02:00
|
|
|
|
|
|
|
statusTextView.setCompoundDrawablesWithIntrinsicBounds(leftImage, null, rightImage, null);
|
2013-02-08 10:09:55 +01:00
|
|
|
|
|
|
|
if (!song.getStarred()) {
|
2013-04-28 01:01:34 +02:00
|
|
|
starImageView.setImageDrawable(Util.getDrawableFromAttribute(getContext(), R.attr.star_hollow));
|
2013-02-08 10:09:55 +01:00
|
|
|
} else {
|
2013-04-28 01:01:34 +02:00
|
|
|
starImageView.setImageDrawable(Util.getDrawableFromAttribute(getContext(), R.attr.star_full));
|
2013-02-08 10:09:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
boolean playing = downloadService.getCurrentPlaying() == downloadFile;
|
|
|
|
if (playing) {
|
2013-04-28 01:01:34 +02:00
|
|
|
titleTextView.setCompoundDrawablesWithIntrinsicBounds(Util.getDrawableFromAttribute(getContext(), R.attr.media_play_small), null, null, null);
|
2013-02-08 10:09:55 +01:00
|
|
|
} else {
|
|
|
|
titleTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setChecked(boolean b) {
|
|
|
|
checkedTextView.setChecked(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isChecked() {
|
|
|
|
return checkedTextView.isChecked();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void toggle() {
|
|
|
|
checkedTextView.toggle();
|
|
|
|
}
|
|
|
|
}
|