From b67ae0a75069d001faf9afe311c7fd1a8c3235f0 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sat, 6 Oct 2012 09:52:19 -0700 Subject: Abstracted out updating of view to UpdateView, moved Song/Album views to View folder --- .../dsub/activity/DownloadActivity.java | 2 +- .../dsub/activity/SubsonicTabActivity.java | 1 + .../github/daneren2005/dsub/util/AlbumView.java | 112 ------------- .../github/daneren2005/dsub/util/EntryAdapter.java | 2 + .../src/github/daneren2005/dsub/util/SongView.java | 185 --------------------- .../github/daneren2005/dsub/view/AlbumView.java | 75 +++++++++ .../src/github/daneren2005/dsub/view/SongView.java | 146 ++++++++++++++++ .../github/daneren2005/dsub/view/UpdateView.java | 75 +++++++++ 8 files changed, 300 insertions(+), 298 deletions(-) delete mode 100644 subsonic-android/src/github/daneren2005/dsub/util/AlbumView.java delete mode 100644 subsonic-android/src/github/daneren2005/dsub/util/SongView.java create mode 100644 subsonic-android/src/github/daneren2005/dsub/view/AlbumView.java create mode 100644 subsonic-android/src/github/daneren2005/dsub/view/SongView.java create mode 100644 subsonic-android/src/github/daneren2005/dsub/view/UpdateView.java (limited to 'subsonic-android/src') diff --git a/subsonic-android/src/github/daneren2005/dsub/activity/DownloadActivity.java b/subsonic-android/src/github/daneren2005/dsub/activity/DownloadActivity.java index bbd4ba2e..0349b017 100644 --- a/subsonic-android/src/github/daneren2005/dsub/activity/DownloadActivity.java +++ b/subsonic-android/src/github/daneren2005/dsub/activity/DownloadActivity.java @@ -70,7 +70,7 @@ import github.daneren2005.dsub.service.MusicServiceFactory; import github.daneren2005.dsub.util.Constants; import github.daneren2005.dsub.util.HorizontalSlider; import github.daneren2005.dsub.util.SilentBackgroundTask; -import github.daneren2005.dsub.util.SongView; +import github.daneren2005.dsub.view.SongView; import github.daneren2005.dsub.util.Util; import github.daneren2005.dsub.view.VisualizerView; diff --git a/subsonic-android/src/github/daneren2005/dsub/activity/SubsonicTabActivity.java b/subsonic-android/src/github/daneren2005/dsub/activity/SubsonicTabActivity.java index 05ad2f4b..7d54908b 100644 --- a/subsonic-android/src/github/daneren2005/dsub/activity/SubsonicTabActivity.java +++ b/subsonic-android/src/github/daneren2005/dsub/activity/SubsonicTabActivity.java @@ -203,6 +203,7 @@ public class SubsonicTabActivity extends SherlockActivity { @Override protected void done(Void result) { + // UpdateView Util.toast(SubsonicTabActivity.this, getResources().getString(starred ? R.string.starring_content_starred : R.string.starring_content_unstarred, entry.getTitle())); } diff --git a/subsonic-android/src/github/daneren2005/dsub/util/AlbumView.java b/subsonic-android/src/github/daneren2005/dsub/util/AlbumView.java deleted file mode 100644 index 124f551d..00000000 --- a/subsonic-android/src/github/daneren2005/dsub/util/AlbumView.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - 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 . - - Copyright 2009 (C) Sindre Mehus - */ -package github.daneren2005.dsub.util; - -import android.content.Context; -import android.os.Handler; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.ImageButton; -import android.widget.LinearLayout; -import android.widget.TextView; -import github.daneren2005.dsub.R; -import github.daneren2005.dsub.domain.MusicDirectory; -import java.util.WeakHashMap; - -/** - * Used to display albums in a {@code ListView}. - * - * @author Sindre Mehus - */ -public class AlbumView extends LinearLayout { - private static final String TAG = AlbumView.class.getSimpleName(); - private static final WeakHashMap INSTANCES = new WeakHashMap(); - private static Handler handler; - - private MusicDirectory.Entry album; - - private TextView titleView; - private TextView artistView; - private View coverArtView; - private ImageButton starButton; - - public AlbumView(Context context) { - super(context); - LayoutInflater.from(context).inflate(R.layout.album_list_item, this, true); - - titleView = (TextView) findViewById(R.id.album_title); - artistView = (TextView) findViewById(R.id.album_artist); - coverArtView = findViewById(R.id.album_coverart); - starButton = (ImageButton) findViewById(R.id.album_star); - - INSTANCES.put(this, null); - int instanceCount = INSTANCES.size(); - if (instanceCount > 50) { - Log.w(TAG, instanceCount + " live AlbumView instances"); - } - startUpdater(); - } - - public void setAlbum(MusicDirectory.Entry album, ImageLoader imageLoader) { - this.album = album; - - titleView.setText(album.getTitle()); - artistView.setText(album.getArtist()); - artistView.setVisibility(album.getArtist() == null ? View.GONE : View.VISIBLE); - imageLoader.loadImage(coverArtView, album, false, true); - - starButton.setVisibility((Util.isOffline(getContext()) || !album.isStarred()) ? View.GONE : View.VISIBLE); - starButton.setFocusable(false); - - update(); - } - - private void update() { - starButton.setVisibility((Util.isOffline(getContext()) || !album.isStarred()) ? View.GONE : View.VISIBLE); - } - - private static synchronized void startUpdater() { - if (handler != null) { - return; - } - - handler = new Handler(); - Runnable runnable = new Runnable() { - @Override - public void run() { - updateAll(); - handler.postDelayed(this, 1000L); - } - }; - handler.postDelayed(runnable, 1000L); - } - - private static void updateAll() { - try { - for (AlbumView view : INSTANCES.keySet()) { - if (view.isShown()) { - view.update(); - } - } - } catch (Throwable x) { - Log.w(TAG, "Error when updating song views.", x); - } - } -} diff --git a/subsonic-android/src/github/daneren2005/dsub/util/EntryAdapter.java b/subsonic-android/src/github/daneren2005/dsub/util/EntryAdapter.java index e66c95cd..bca05e00 100644 --- a/subsonic-android/src/github/daneren2005/dsub/util/EntryAdapter.java +++ b/subsonic-android/src/github/daneren2005/dsub/util/EntryAdapter.java @@ -18,6 +18,8 @@ */ package github.daneren2005.dsub.util; +import github.daneren2005.dsub.view.AlbumView; +import github.daneren2005.dsub.view.SongView; import java.util.List; import android.view.View; diff --git a/subsonic-android/src/github/daneren2005/dsub/util/SongView.java b/subsonic-android/src/github/daneren2005/dsub/util/SongView.java deleted file mode 100644 index 24c483dc..00000000 --- a/subsonic-android/src/github/daneren2005/dsub/util/SongView.java +++ /dev/null @@ -1,185 +0,0 @@ -/* - 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 . - - Copyright 2009 (C) Sindre Mehus - */ -package github.daneren2005.dsub.util; - -import android.content.Context; -import android.os.Handler; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.Checkable; -import android.widget.CheckedTextView; -import android.widget.ImageButton; -import android.widget.LinearLayout; -import android.widget.TextView; -import github.daneren2005.dsub.R; -import github.daneren2005.dsub.activity.SubsonicTabActivity; -import github.daneren2005.dsub.domain.MusicDirectory; -import github.daneren2005.dsub.service.DownloadService; -import github.daneren2005.dsub.service.DownloadServiceImpl; -import github.daneren2005.dsub.service.DownloadFile; - -import java.io.File; -import java.util.WeakHashMap; - -/** - * Used to display songs in a {@code ListView}. - * - * @author Sindre Mehus - */ -public class SongView extends LinearLayout implements Checkable { - private static final String TAG = SongView.class.getSimpleName(); - private static final WeakHashMap INSTANCES = new WeakHashMap(); - private static Handler handler; - - private MusicDirectory.Entry song; - - private CheckedTextView checkedTextView; - private TextView titleTextView; - private TextView artistTextView; - private TextView durationTextView; - private TextView statusTextView; - private ImageButton starButton; - - public SongView(Context context) { - super(context); - LayoutInflater.from(context).inflate(R.layout.song_list_item, this, true); - - checkedTextView = (CheckedTextView) findViewById(R.id.song_check); - 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); - starButton = (ImageButton) findViewById(R.id.song_star); - - INSTANCES.put(this, null); - int instanceCount = INSTANCES.size(); - if (instanceCount > 50) { - Log.w(TAG, instanceCount + " live SongView instances"); - } - startUpdater(); - } - - public void setSong(MusicDirectory.Entry song, boolean checkable) { - this.song = song; - StringBuilder artist = new StringBuilder(40); - - String bitRate = null; - if (song.getBitRate() != null) { - bitRate = String.format(getContext().getString(R.string.song_details_kbps), song.getBitRate()); - } - - String fileFormat = null; - if (song.getTranscodedSuffix() != null && !song.getTranscodedSuffix().equals(song.getSuffix())) { - fileFormat = String.format("%s > %s", song.getSuffix(), song.getTranscodedSuffix()); - } else { - fileFormat = song.getSuffix(); - } - - artist.append(song.getArtist()).append(" (") - .append(String.format(getContext().getString(R.string.song_details_all), bitRate == null ? "" : bitRate, fileFormat)) - .append(")"); - - titleTextView.setText(song.getTitle()); - artistTextView.setText(artist); - durationTextView.setText(Util.formatDuration(song.getDuration())); - checkedTextView.setVisibility(checkable && !song.isVideo() ? View.VISIBLE : View.GONE); - starButton.setVisibility((Util.isOffline(getContext()) || !song.isStarred()) ? View.GONE : View.VISIBLE); - starButton.setFocusable(false); - - update(); - } - - private void update() { - DownloadService downloadService = DownloadServiceImpl.getInstance(); - if (downloadService == null) { - return; - } - - DownloadFile downloadFile = downloadService.forSong(song); - File completeFile = downloadFile.getCompleteFile(); - File partialFile = downloadFile.getPartialFile(); - - int leftImage = 0; - int rightImage = 0; - - if (completeFile.exists()) { - leftImage = downloadFile.isSaved() ? R.drawable.saved : R.drawable.downloaded; - } - - if (downloadFile.isDownloading() && !downloadFile.isDownloadCancelled() && partialFile.exists()) { - statusTextView.setText(Util.formatLocalizedBytes(partialFile.length(), getContext())); - rightImage = R.drawable.downloading; - } else { - statusTextView.setText(null); - } - statusTextView.setCompoundDrawablesWithIntrinsicBounds(leftImage, 0, rightImage, 0); - - boolean playing = downloadService.getCurrentPlaying() == downloadFile; - if (playing) { - titleTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.stat_notify_playing, 0, 0, 0); - } else { - titleTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); - } - starButton.setVisibility((Util.isOffline(getContext()) || !song.isStarred()) ? View.GONE : View.VISIBLE); - } - - private static synchronized void startUpdater() { - if (handler != null) { - return; - } - - handler = new Handler(); - Runnable runnable = new Runnable() { - @Override - public void run() { - updateAll(); - handler.postDelayed(this, 1000L); - } - }; - handler.postDelayed(runnable, 1000L); - } - - private static void updateAll() { - try { - for (SongView view : INSTANCES.keySet()) { - if (view.isShown()) { - view.update(); - } - } - } catch (Throwable x) { - Log.w(TAG, "Error when updating song views.", x); - } - } - - @Override - public void setChecked(boolean b) { - checkedTextView.setChecked(b); - } - - @Override - public boolean isChecked() { - return checkedTextView.isChecked(); - } - - @Override - public void toggle() { - checkedTextView.toggle(); - } -} diff --git a/subsonic-android/src/github/daneren2005/dsub/view/AlbumView.java b/subsonic-android/src/github/daneren2005/dsub/view/AlbumView.java new file mode 100644 index 00000000..ea8f5110 --- /dev/null +++ b/subsonic-android/src/github/daneren2005/dsub/view/AlbumView.java @@ -0,0 +1,75 @@ +/* + 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 . + + Copyright 2009 (C) Sindre Mehus + */ +package github.daneren2005.dsub.view; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.ImageButton; +import android.widget.TextView; +import github.daneren2005.dsub.R; +import github.daneren2005.dsub.domain.MusicDirectory; +import github.daneren2005.dsub.util.ImageLoader; +import github.daneren2005.dsub.util.Util; +import github.daneren2005.dsub.view.UpdateView; + +/** + * Used to display albums in a {@code ListView}. + * + * @author Sindre Mehus + */ +public class AlbumView extends UpdateView { + private static final String TAG = AlbumView.class.getSimpleName(); + + private MusicDirectory.Entry album; + + private TextView titleView; + private TextView artistView; + private View coverArtView; + private ImageButton starButton; + + public AlbumView(Context context) { + super(context); + LayoutInflater.from(context).inflate(R.layout.album_list_item, this, true); + + titleView = (TextView) findViewById(R.id.album_title); + artistView = (TextView) findViewById(R.id.album_artist); + coverArtView = findViewById(R.id.album_coverart); + starButton = (ImageButton) findViewById(R.id.album_star); + } + + public void setAlbum(MusicDirectory.Entry album, ImageLoader imageLoader) { + this.album = album; + + titleView.setText(album.getTitle()); + artistView.setText(album.getArtist()); + artistView.setVisibility(album.getArtist() == null ? View.GONE : View.VISIBLE); + imageLoader.loadImage(coverArtView, album, false, true); + + starButton.setVisibility((Util.isOffline(getContext()) || !album.isStarred()) ? View.GONE : View.VISIBLE); + starButton.setFocusable(false); + + update(); + } + + @Override + protected void update() { + starButton.setVisibility((Util.isOffline(getContext()) || !album.isStarred()) ? View.GONE : View.VISIBLE); + } +} diff --git a/subsonic-android/src/github/daneren2005/dsub/view/SongView.java b/subsonic-android/src/github/daneren2005/dsub/view/SongView.java new file mode 100644 index 00000000..13f88a3e --- /dev/null +++ b/subsonic-android/src/github/daneren2005/dsub/view/SongView.java @@ -0,0 +1,146 @@ +/* + 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 . + + Copyright 2009 (C) Sindre Mehus + */ +package github.daneren2005.dsub.view; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.Checkable; +import android.widget.CheckedTextView; +import android.widget.ImageButton; +import android.widget.TextView; +import github.daneren2005.dsub.R; +import github.daneren2005.dsub.domain.MusicDirectory; +import github.daneren2005.dsub.service.DownloadService; +import github.daneren2005.dsub.service.DownloadServiceImpl; +import github.daneren2005.dsub.service.DownloadFile; +import github.daneren2005.dsub.util.Util; +import github.daneren2005.dsub.view.UpdateView; + +import java.io.File; + +/** + * Used to display songs in a {@code ListView}. + * + * @author Sindre Mehus + */ +public class SongView extends UpdateView implements Checkable { + private static final String TAG = SongView.class.getSimpleName(); + + private MusicDirectory.Entry song; + + private CheckedTextView checkedTextView; + private TextView titleTextView; + private TextView artistTextView; + private TextView durationTextView; + private TextView statusTextView; + private ImageButton starButton; + + public SongView(Context context) { + super(context); + LayoutInflater.from(context).inflate(R.layout.song_list_item, this, true); + + checkedTextView = (CheckedTextView) findViewById(R.id.song_check); + 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); + starButton = (ImageButton) findViewById(R.id.song_star); + } + + public void setSong(MusicDirectory.Entry song, boolean checkable) { + this.song = song; + StringBuilder artist = new StringBuilder(40); + + String bitRate = null; + if (song.getBitRate() != null) { + bitRate = String.format(getContext().getString(R.string.song_details_kbps), song.getBitRate()); + } + + String fileFormat = null; + if (song.getTranscodedSuffix() != null && !song.getTranscodedSuffix().equals(song.getSuffix())) { + fileFormat = String.format("%s > %s", song.getSuffix(), song.getTranscodedSuffix()); + } else { + fileFormat = song.getSuffix(); + } + + artist.append(song.getArtist()).append(" (") + .append(String.format(getContext().getString(R.string.song_details_all), bitRate == null ? "" : bitRate, fileFormat)) + .append(")"); + + titleTextView.setText(song.getTitle()); + artistTextView.setText(artist); + durationTextView.setText(Util.formatDuration(song.getDuration())); + checkedTextView.setVisibility(checkable && !song.isVideo() ? View.VISIBLE : View.GONE); + starButton.setVisibility((Util.isOffline(getContext()) || !song.isStarred()) ? View.GONE : View.VISIBLE); + starButton.setFocusable(false); + + update(); + } + + @Override + protected void update() { + DownloadService downloadService = DownloadServiceImpl.getInstance(); + if (downloadService == null) { + return; + } + + DownloadFile downloadFile = downloadService.forSong(song); + File completeFile = downloadFile.getCompleteFile(); + File partialFile = downloadFile.getPartialFile(); + + int leftImage = 0; + int rightImage = 0; + + if (completeFile.exists()) { + leftImage = downloadFile.isSaved() ? R.drawable.saved : R.drawable.downloaded; + } + + if (downloadFile.isDownloading() && !downloadFile.isDownloadCancelled() && partialFile.exists()) { + statusTextView.setText(Util.formatLocalizedBytes(partialFile.length(), getContext())); + rightImage = R.drawable.downloading; + } else { + statusTextView.setText(null); + } + statusTextView.setCompoundDrawablesWithIntrinsicBounds(leftImage, 0, rightImage, 0); + + boolean playing = downloadService.getCurrentPlaying() == downloadFile; + if (playing) { + titleTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.stat_notify_playing, 0, 0, 0); + } else { + titleTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); + } + starButton.setVisibility((Util.isOffline(getContext()) || !song.isStarred()) ? View.GONE : View.VISIBLE); + } + + @Override + public void setChecked(boolean b) { + checkedTextView.setChecked(b); + } + + @Override + public boolean isChecked() { + return checkedTextView.isChecked(); + } + + @Override + public void toggle() { + checkedTextView.toggle(); + } +} diff --git a/subsonic-android/src/github/daneren2005/dsub/view/UpdateView.java b/subsonic-android/src/github/daneren2005/dsub/view/UpdateView.java new file mode 100644 index 00000000..d4d6081c --- /dev/null +++ b/subsonic-android/src/github/daneren2005/dsub/view/UpdateView.java @@ -0,0 +1,75 @@ +/* + 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 . + + Copyright 2009 (C) Sindre Mehus + */ +package github.daneren2005.dsub.view; + +import android.content.Context; +import android.os.Handler; +import android.util.Log; +import android.widget.LinearLayout; +import java.util.WeakHashMap; + +public class UpdateView extends LinearLayout { + private static final String TAG = UpdateView.class.getSimpleName(); + private static final WeakHashMap INSTANCES = new WeakHashMap(); + private static Handler handler; + + public UpdateView(Context context) { + super(context); + + INSTANCES.put(this, null); + int instanceCount = INSTANCES.size(); + if (instanceCount > 50) { + Log.w(TAG, instanceCount + " live UpdateView instances"); + } + + startUpdater(); + } + + private static synchronized void startUpdater() { + if (handler != null) { + return; + } + + handler = new Handler(); + Runnable runnable = new Runnable() { + @Override + public void run() { + updateAll(); + handler.postDelayed(this, 1000L); + } + }; + handler.postDelayed(runnable, 1000L); + } + + private static void updateAll() { + try { + for (UpdateView view : INSTANCES.keySet()) { + if (view.isShown()) { + view.update(); + } + } + } catch (Throwable x) { + Log.w(TAG, "Error when updating song views.", x); + } + } + + protected void update() { + + } +} -- cgit v1.2.3