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-12-21 06:15:27 +01:00
|
|
|
import android.graphics.drawable.AnimationDrawable;
|
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-12-04 07:36:02 +01:00
|
|
|
|
2013-04-06 21:47:24 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.R;
|
2013-06-04 09:37:53 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.MusicDirectory.Entry;
|
2013-12-04 07:36:02 +01:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.service.DownloadFile;
|
2013-04-06 21:47:24 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.service.DownloadService;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.service.DownloadServiceImpl;
|
|
|
|
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-12-04 07:36:02 +01:00
|
|
|
public class SongView extends UpdateView implements Checkable
|
|
|
|
{
|
|
|
|
|
|
|
|
private static final String TAG = SongView.class.getSimpleName();
|
|
|
|
private static Drawable starHollowDrawable;
|
|
|
|
private static Drawable starDrawable;
|
|
|
|
private static Drawable unpinImage;
|
|
|
|
private static Drawable downloadedImage;
|
|
|
|
private static Drawable downloadingImage;
|
|
|
|
private static Drawable playingImage;
|
|
|
|
private static String theme;
|
|
|
|
private static LayoutInflater inflater;
|
|
|
|
|
|
|
|
private CheckedTextView checkedTextView;
|
|
|
|
private ImageView starImageView;
|
2013-12-21 17:58:21 +01:00
|
|
|
private ImageView songDragImageView;
|
2013-12-04 07:36:02 +01:00
|
|
|
private TextView titleTextView;
|
|
|
|
private TextView statusTextView;
|
|
|
|
private Entry song;
|
|
|
|
private Context context;
|
|
|
|
private Drawable leftImage;
|
|
|
|
private ImageType previousLeftImageType;
|
|
|
|
private ImageType previousRightImageType;
|
|
|
|
private ImageType leftImageType;
|
|
|
|
private ImageType rightImageType;
|
|
|
|
private Drawable rightImage;
|
2013-05-16 09:59:55 +02:00
|
|
|
private DownloadService downloadService;
|
2013-12-04 07:36:02 +01:00
|
|
|
private DownloadFile downloadFile;
|
|
|
|
private boolean playing;
|
|
|
|
|
|
|
|
public SongView(Context context)
|
|
|
|
{
|
|
|
|
super(context);
|
|
|
|
this.context = context;
|
|
|
|
|
|
|
|
String theme = Util.getTheme(context);
|
|
|
|
boolean themesMatch = theme.equals(SongView.theme);
|
|
|
|
inflater = LayoutInflater.from(this.context);
|
|
|
|
|
|
|
|
if (!themesMatch)
|
|
|
|
{
|
|
|
|
SongView.theme = theme;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (starHollowDrawable == null || !themesMatch)
|
|
|
|
{
|
|
|
|
starHollowDrawable = Util.getDrawableFromAttribute(context, R.attr.star_hollow);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (starDrawable == null || !themesMatch)
|
|
|
|
{
|
|
|
|
starDrawable = Util.getDrawableFromAttribute(context, R.attr.star_full);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unpinImage == null || !themesMatch)
|
|
|
|
{
|
|
|
|
unpinImage = Util.getDrawableFromAttribute(context, R.attr.unpin);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (downloadedImage == null || !themesMatch)
|
|
|
|
{
|
|
|
|
downloadedImage = Util.getDrawableFromAttribute(context, R.attr.downloaded);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (downloadingImage == null || !themesMatch)
|
|
|
|
{
|
|
|
|
downloadingImage = Util.getDrawableFromAttribute(context, R.attr.downloading);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (playingImage == null || !themesMatch)
|
|
|
|
{
|
|
|
|
playingImage = Util.getDrawableFromAttribute(context, R.attr.media_play_small);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Entry getEntry()
|
|
|
|
{
|
|
|
|
return this.song;
|
|
|
|
}
|
|
|
|
|
2013-12-21 17:58:21 +01:00
|
|
|
protected void setSong(final Entry song, boolean checkable, boolean dragable)
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
|
|
|
updateBackground();
|
|
|
|
|
|
|
|
this.song = song;
|
2013-12-07 03:43:13 +01:00
|
|
|
|
|
|
|
if (downloadService != null)
|
|
|
|
{
|
|
|
|
this.downloadFile = downloadService.forSong(song);
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
inflater.inflate(song.isVideo() ? R.layout.video_list_item : R.layout.song_list_item, this, true);
|
|
|
|
|
|
|
|
checkedTextView = (CheckedTextView) findViewById(R.id.song_check);
|
|
|
|
starImageView = (ImageView) findViewById(R.id.song_star);
|
2013-12-21 17:58:21 +01:00
|
|
|
songDragImageView = (ImageView) findViewById(R.id.song_drag);
|
2013-12-04 07:36:02 +01:00
|
|
|
TextView trackTextView = (TextView) findViewById(R.id.song_track);
|
|
|
|
titleTextView = (TextView) findViewById(R.id.song_title);
|
|
|
|
TextView artistTextView = (TextView) findViewById(R.id.song_artist);
|
|
|
|
TextView durationTextView = (TextView) findViewById(R.id.song_duration);
|
|
|
|
statusTextView = (TextView) findViewById(R.id.song_status);
|
|
|
|
|
|
|
|
StringBuilder artist = new StringBuilder(60);
|
|
|
|
|
|
|
|
String bitRate = null;
|
|
|
|
|
|
|
|
if (song.getBitRate() != null)
|
|
|
|
{
|
|
|
|
bitRate = String.format(this.context.getString(R.string.song_details_kbps), song.getBitRate());
|
|
|
|
}
|
|
|
|
|
|
|
|
String fileFormat;
|
|
|
|
String suffix = song.getSuffix();
|
|
|
|
String transcodedSuffix = song.getTranscodedSuffix();
|
|
|
|
|
|
|
|
fileFormat = transcodedSuffix == null || transcodedSuffix.equals(suffix) || (song.isVideo() && Util.getVideoPlayerType(this.context) != VideoPlayerType.FLASH) ? suffix : String.format("%s > %s", suffix, transcodedSuffix);
|
|
|
|
|
|
|
|
String artistName = song.getArtist();
|
|
|
|
|
|
|
|
if (artistName != null)
|
|
|
|
{
|
|
|
|
if (Util.shouldDisplayBitrateWithArtist(this.context))
|
|
|
|
{
|
|
|
|
artist.append(artistName).append(" (").append(String.format(this.context.getString(R.string.song_details_all), bitRate == null ? "" : String.format("%s ", bitRate), fileFormat)).append(')');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
artist.append(artistName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int trackNumber = song.getTrack();
|
|
|
|
|
|
|
|
if (trackTextView != null)
|
|
|
|
{
|
|
|
|
if (Util.shouldShowTrackNumber(this.context) && trackNumber != 0)
|
|
|
|
{
|
|
|
|
trackTextView.setText(String.format("%02d.", trackNumber));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
trackTextView.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
titleTextView.setText(song.getTitle());
|
|
|
|
|
|
|
|
if (artistTextView != null)
|
|
|
|
{
|
|
|
|
artistTextView.setText(artist);
|
|
|
|
}
|
2013-07-18 09:58:59 +02:00
|
|
|
|
|
|
|
Integer duration = song.getDuration();
|
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
if (duration != null)
|
|
|
|
{
|
|
|
|
durationTextView.setText(Util.formatTotalDuration(duration));
|
2013-07-18 09:58:59 +02:00
|
|
|
}
|
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
checkedTextView.setVisibility(checkable && !song.isVideo() ? View.VISIBLE : View.GONE);
|
2013-12-21 17:58:21 +01:00
|
|
|
songDragImageView.setVisibility(dragable ? View.VISIBLE : View.GONE);
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
if (Util.isOffline(this.context))
|
|
|
|
{
|
|
|
|
starImageView.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
starImageView.setImageDrawable(song.getStarred() ? starDrawable : starHollowDrawable);
|
|
|
|
|
|
|
|
starImageView.setOnClickListener(new View.OnClickListener()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void onClick(View view)
|
|
|
|
{
|
|
|
|
final boolean isStarred = song.getStarred();
|
|
|
|
final String id = song.getId();
|
|
|
|
|
|
|
|
if (!isStarred)
|
|
|
|
{
|
|
|
|
starImageView.setImageDrawable(starDrawable);
|
|
|
|
song.setStarred(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
starImageView.setImageDrawable(starHollowDrawable);
|
|
|
|
song.setStarred(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
new Thread(new Runnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
MusicService musicService = MusicServiceFactory.getMusicService(SongView.this.context);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!isStarred)
|
|
|
|
{
|
|
|
|
musicService.star(id, null, null, SongView.this.context, null);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
musicService.unstar(id, null, null, SongView.this.context, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Log.e(TAG, e.getMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
@Override
|
2013-12-04 07:36:02 +01:00
|
|
|
protected void updateBackground()
|
|
|
|
{
|
|
|
|
if (downloadService == null)
|
|
|
|
{
|
2013-05-16 09:59:55 +02:00
|
|
|
downloadService = DownloadServiceImpl.getInstance();
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void update()
|
|
|
|
{
|
|
|
|
updateBackground();
|
|
|
|
|
|
|
|
if (downloadService == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadFile = downloadService.forSong(this.song);
|
|
|
|
File partialFile = downloadFile.getPartialFile();
|
|
|
|
|
|
|
|
if (downloadFile.isWorkDone())
|
|
|
|
{
|
|
|
|
ImageType newLeftImageType = downloadFile.isSaved() ? ImageType.unpin : ImageType.downloaded;
|
|
|
|
|
|
|
|
if (this.leftImageType != newLeftImageType)
|
|
|
|
{
|
|
|
|
this.leftImage = downloadFile.isSaved() ? unpinImage : downloadedImage;
|
|
|
|
this.leftImageType = newLeftImageType;
|
2013-05-16 09:59:55 +02:00
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.leftImageType = ImageType.none;
|
|
|
|
this.leftImage = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (downloadFile.isDownloading() && !downloadFile.isDownloadCancelled() && partialFile.exists())
|
|
|
|
{
|
2013-12-08 08:41:30 +01:00
|
|
|
if (this.statusTextView != null)
|
|
|
|
{
|
|
|
|
this.statusTextView.setText(Util.formatLocalizedBytes(partialFile.length(), this.context));
|
|
|
|
}
|
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
this.rightImageType = ImageType.downloading;
|
|
|
|
this.rightImage = downloadingImage;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.rightImageType = ImageType.none;
|
|
|
|
this.rightImage = null;
|
|
|
|
|
2013-12-07 03:43:13 +01:00
|
|
|
if (this.statusTextView != null)
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
2013-12-07 03:43:13 +01:00
|
|
|
CharSequence statusText = this.statusTextView.getText();
|
|
|
|
|
|
|
|
if (statusText != "" || statusText != null)
|
|
|
|
{
|
|
|
|
this.statusTextView.setText(null);
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.previousLeftImageType != leftImageType || this.previousRightImageType != rightImageType)
|
|
|
|
{
|
|
|
|
this.previousLeftImageType = leftImageType;
|
|
|
|
this.previousRightImageType = rightImageType;
|
2013-12-08 08:41:30 +01:00
|
|
|
|
|
|
|
if (this.statusTextView != null)
|
|
|
|
{
|
|
|
|
this.statusTextView.setCompoundDrawablesWithIntrinsicBounds(leftImage, null, rightImage, null);
|
2013-12-21 06:15:27 +01:00
|
|
|
|
|
|
|
if (rightImage == downloadingImage)
|
|
|
|
{
|
|
|
|
AnimationDrawable frameAnimation = (AnimationDrawable) rightImage;
|
|
|
|
frameAnimation.setVisible(true, true);
|
|
|
|
}
|
2013-12-08 08:41:30 +01:00
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!song.getStarred())
|
|
|
|
{
|
2013-12-08 08:41:30 +01:00
|
|
|
if (starImageView != null)
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
2013-12-08 08:41:30 +01:00
|
|
|
if (starImageView.getDrawable() != starHollowDrawable)
|
|
|
|
{
|
|
|
|
starImageView.setImageDrawable(starHollowDrawable);
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-08 08:41:30 +01:00
|
|
|
if (starImageView != null)
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
2013-12-08 08:41:30 +01:00
|
|
|
if (starImageView.getDrawable() != starDrawable)
|
|
|
|
{
|
|
|
|
starImageView.setImageDrawable(starDrawable);
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean playing = downloadService.getCurrentPlaying() == downloadFile;
|
|
|
|
|
|
|
|
if (playing)
|
|
|
|
{
|
|
|
|
if (!this.playing)
|
|
|
|
{
|
|
|
|
this.playing = true;
|
|
|
|
titleTextView.setCompoundDrawablesWithIntrinsicBounds(playingImage, null, null, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this.playing)
|
|
|
|
{
|
|
|
|
this.playing = false;
|
|
|
|
titleTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setChecked(boolean b)
|
|
|
|
{
|
|
|
|
checkedTextView.setChecked(b);
|
2013-05-16 09:59:55 +02:00
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-05-16 09:59:55 +02:00
|
|
|
@Override
|
2013-12-04 07:36:02 +01:00
|
|
|
public boolean isChecked()
|
|
|
|
{
|
|
|
|
return checkedTextView.isChecked();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void toggle()
|
|
|
|
{
|
|
|
|
checkedTextView.toggle();
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum ImageType
|
|
|
|
{
|
|
|
|
none,
|
|
|
|
unpin,
|
|
|
|
downloaded,
|
|
|
|
downloading
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
}
|