From 2b7ce9cb2ca10daaccc79ce689562b6ef5feaa0b Mon Sep 17 00:00:00 2001 From: Joshua Bahnsen Date: Mon, 20 Jan 2014 23:16:24 -0700 Subject: [PATCH] Improve lock screen reliability, add 4.3 specific lockscreen features, add scanning of media after download, improve image caching --- res/values-fr/strings.xml | 2 + res/values-hu/strings.xml | 2 + res/values/strings.xml | 2 + res/xml/settings.xml | 117 +++---- .../activity/SubsonicTabActivity.java | 2 +- .../androidapp/service/DownloadFile.java | 16 +- .../service/DownloadServiceImpl.java | 297 ++++++++++-------- .../service/parser/IndexesParser.java | 6 +- .../ultrasonic/androidapp/util/Constants.java | 1 + .../ultrasonic/androidapp/util/FileUtil.java | 17 +- .../androidapp/util/ImageLoader.java | 39 ++- .../ultrasonic/androidapp/util/Util.java | 14 + 12 files changed, 319 insertions(+), 196 deletions(-) diff --git a/res/values-fr/strings.xml b/res/values-fr/strings.xml index 2086da00..f347b258 100644 --- a/res/values-fr/strings.xml +++ b/res/values-fr/strings.xml @@ -372,6 +372,8 @@ Voir toutes les titres par artiste Ajouter une nouvelle entrée de l\'affichage de l\'artiste pour accéder à toutes les titres pour un artiste Afficher l\'artiste + Scan Media After Download + Automatically scan media after download Aucun titre diff --git a/res/values-hu/strings.xml b/res/values-hu/strings.xml index 00c3b5e7..71d3210b 100644 --- a/res/values-hu/strings.xml +++ b/res/values-hu/strings.xml @@ -372,6 +372,8 @@ Az előadó összes dalának megjelenítése Új bejegyzés hozzáadása az előadóhoz, az előadó összes dalának eléréséhez. Ugrás az előadóhoz + Scan Media After Download + Automatically scan media after download Nincsenek dalok diff --git a/res/values/strings.xml b/res/values/strings.xml index 56ee3913..f1ec7eea 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -372,6 +372,8 @@ Show All Songs By Artist Add new entry in artist view to access all songs for an artist Show Artist + Scan Media After Download + Automatically scan media after download No songs diff --git a/res/xml/settings.xml b/res/xml/settings.xml index 4bd05931..c2e89b19 100644 --- a/res/xml/settings.xml +++ b/res/xml/settings.xml @@ -1,33 +1,33 @@ + a:title="@string/common.appname"> + a:title="@string/settings.servers_title"> - + + a:title="@string/settings.theme_title"/> + a:title="@string/settings.server_scaling_title"/> + a:title="@string/settings.display_bitrate"/> + a:title="@string/settings.use_folder_for_album_artist"/> + a:title="@string/settings.show_track_number"/> + a:title="@string/settings.disc_sort"/> + a:title="@string/settings.view_refresh"/> - + a:title="@string/settings.use_id3"/> + a:title="@string/settings.media_button_title"/> + a:title="@string/settings.download_transition"/> + a:title="@string/settings.gapless_playback"/> + a:title="@string/settings.clear_playlist"/> + a:title="@string/settings.clear_bookmark"/> + + a:title="@string/settings.increment_time"/> - + + a:title="@string/settings.show_now_playing"/> + a:title="@string/settings.show_notification"/> + a:title="@string/settings.show_notification_always"/> + a:title="@string/settings.show_lockscreen_controls"/> + a:title="@string/settings.send_bluetooth_notification"/> + a:title="@string/settings.send_bluetooth_album_art"/> - + + a:title="@string/settings.video_player"/> - + + a:title="@string/settings.share_description_default"/> + a:title="@string/settings.share_greeting_default"/> + a:title="@string/settings.share_expiration_default"/> + a:title="@string/settings.sharing_always_ask_for_details"/> - + + a:title="@string/settings.max_bitrate_wifi"/> + a:title="@string/settings.max_bitrate_mobile"/> + a:title="@string/settings.wifi_required_title"/> + a:title="@string/settings.buffer_length"/> + a:title="@string/settings.network_timeout"/> + a:title="@string/settings.chat_refresh"/> - + + a:title="@string/settings.cache_size"/> + a:title="@string/settings.cache_location"/> + a:title="@string/settings.preload"/> + a:title="@string/settings.directory_cache_time"/> - + + a:title="@string/settings.default_artists"/> + a:title="@string/settings.max_artists"/> + a:title="@string/settings.default_albums"/> + a:title="@string/settings.max_albums"/> + a:title="@string/settings.default_songs"/> + a:title="@string/settings.max_songs"/> + a:title="@string/settings.clear_search_history"/> - + + a:title="@string/settings.scrobble_title"/> + a:title="@string/settings.hide_media_title"/> + a:title="@string/settings.screen_lit_title"/> \ No newline at end of file diff --git a/src/com/thejoshwa/ultrasonic/androidapp/activity/SubsonicTabActivity.java b/src/com/thejoshwa/ultrasonic/androidapp/activity/SubsonicTabActivity.java index aa628daf..a0bb09da 100644 --- a/src/com/thejoshwa/ultrasonic/androidapp/activity/SubsonicTabActivity.java +++ b/src/com/thejoshwa/ultrasonic/androidapp/activity/SubsonicTabActivity.java @@ -957,7 +957,7 @@ public class SubsonicTabActivity extends ResultActivity implements OnClickListen } } - protected synchronized ImageLoader getImageLoader() + public synchronized ImageLoader getImageLoader() { if (IMAGE_LOADER == null) { diff --git a/src/com/thejoshwa/ultrasonic/androidapp/service/DownloadFile.java b/src/com/thejoshwa/ultrasonic/androidapp/service/DownloadFile.java index 4d822a14..488ea343 100644 --- a/src/com/thejoshwa/ultrasonic/androidapp/service/DownloadFile.java +++ b/src/com/thejoshwa/ultrasonic/androidapp/service/DownloadFile.java @@ -297,11 +297,9 @@ public class DownloadFile private class DownloadTask extends CancellableTask { - @Override public void execute() { - InputStream in = null; FileOutputStream out = null; PowerManager.WakeLock wakeLock = null; @@ -417,19 +415,29 @@ public class DownloadFile { Util.renameFile(partialFile, saveFile); mediaStoreService.saveInMediaStore(DownloadFile.this); + + if (Util.getShouldScanMedia(context)) + { + Util.scanMedia(context, saveFile); + } } else { Util.renameFile(partialFile, completeFile); + + if (Util.getShouldScanMedia(context)) + { + Util.scanMedia(context, completeFile); + } } } - } catch (Exception x) { Util.close(out); Util.delete(completeFile); Util.delete(saveFile); + if (!isCancelled()) { failed = true; @@ -450,7 +458,9 @@ public class DownloadFile { wifiLock.release(); } + new CacheCleaner(context, DownloadServiceImpl.getInstance()).cleanSpace(); + if (DownloadServiceImpl.getInstance() != null) { ((DownloadServiceImpl) DownloadServiceImpl.getInstance()).checkDownloads(); diff --git a/src/com/thejoshwa/ultrasonic/androidapp/service/DownloadServiceImpl.java b/src/com/thejoshwa/ultrasonic/androidapp/service/DownloadServiceImpl.java index 27f4a354..5dee3eed 100644 --- a/src/com/thejoshwa/ultrasonic/androidapp/service/DownloadServiceImpl.java +++ b/src/com/thejoshwa/ultrasonic/androidapp/service/DownloadServiceImpl.java @@ -136,7 +136,6 @@ public class DownloadServiceImpl extends Service implements DownloadService private boolean jukeboxEnabled; private PositionCache positionCache; private StreamProxy proxy; - private static MusicDirectory.Entry currentSong; private RemoteControlClient remoteControlClient; private AudioManager audioManager; private int secondaryProgress = -1; @@ -220,6 +219,11 @@ public class DownloadServiceImpl extends Service implements DownloadService audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); + if (Util.isLockScreenEnabled(this)) + { + setUpRemoteControlClient(); + } + notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT; notification.contentView = new RemoteViews(this.getPackageName(), R.layout.notification); Util.linkButtons(this, notification.contentView, false); @@ -275,51 +279,54 @@ public class DownloadServiceImpl extends Service implements DownloadService { super.onDestroy(); - instance = null; - lifecycleSupport.onDestroy(); - mediaPlayer.release(); - - if (nextMediaPlayer != null) - { - nextMediaPlayer.release(); - } - - mediaPlayerLooper.quit(); - shufflePlayBuffer.shutdown(); - - if (equalizerController != null) - { - equalizerController.release(); - } - if (visualizerController != null) - { - visualizerController.release(); - } - try { + instance = null; + lifecycleSupport.onDestroy(); + mediaPlayer.release(); + + if (nextMediaPlayer != null) + { + nextMediaPlayer.release(); + } + + mediaPlayerLooper.quit(); + shufflePlayBuffer.shutdown(); + + if (equalizerController != null) + { + equalizerController.release(); + } + + if (visualizerController != null) + { + visualizerController.release(); + } + + if (bufferTask != null) + { + bufferTask.cancel(); + } + + if (nextPlayingTask != null) + { + nextPlayingTask.cancel(); + } + + notification = null; + Intent i = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION); i.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mediaPlayer.getAudioSessionId()); i.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName()); sendBroadcast(i); - } - catch (Throwable e) - { - // Froyo or lower - } - if (bufferTask != null) - { - bufferTask.cancel(); - } + audioManager.unregisterRemoteControlClient(remoteControlClient); - if (nextPlayingTask != null) - { - nextPlayingTask.cancel(); + wakeLock.release(); + } + catch (Throwable ignored) + { } - - audioManager.unregisterRemoteControlClient(remoteControlClient); - notification = null; } public static DownloadService getInstance() @@ -958,6 +965,8 @@ public class DownloadServiceImpl extends Service implements DownloadService { mediaPlayer.seekTo(position); cachedPosition = position; + + updateRemoteControl(); } } catch (Exception x) @@ -1213,8 +1222,11 @@ public class DownloadServiceImpl extends Service implements DownloadService Util.broadcastPlaybackStatusChange(this, this.playerState); Util.broadcastA2dpPlayStatusChange(this, this.playerState, instance); - // Set remote control - updateRemoteControl(); + if (this.playerState == PlayerState.STARTED || this.playerState == PlayerState.PAUSED) + { + // Set remote control + updateRemoteControl(); + } // Update widget UltraSonicAppWidgetProvider4x1.getInstance().notifyChange(this, this, this.playerState == PlayerState.STARTED, false); @@ -1423,98 +1435,46 @@ public class DownloadServiceImpl extends Service implements DownloadService jukeboxService.adjustVolume(up); } + @SuppressLint("NewApi") + private void setUpRemoteControlClient() + { + final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); + mediaButtonIntent.setComponent(new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName())); + remoteControlClient = new RemoteControlClient(PendingIntent.getBroadcast(this, 0, mediaButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT)); + audioManager.registerRemoteControlClient(remoteControlClient); + + // Flags for the media transport control that this client supports. + int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_STOP; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) + { + flags |= RemoteControlClient.FLAG_KEY_MEDIA_POSITION_UPDATE; + + remoteControlClient.setOnGetPlaybackPositionListener(new RemoteControlClient.OnGetPlaybackPositionListener() + { + @Override + public long onGetPlaybackPosition() + { + return mediaPlayer.getCurrentPosition(); + } + }); + + remoteControlClient.setPlaybackPositionUpdateListener(new RemoteControlClient.OnPlaybackPositionUpdateListener() + { + @Override + public void onPlaybackPositionUpdate(long newPositionMs) + { + seekTo((int) newPositionMs); + } + }); + } + + remoteControlClient.setTransportControlFlags(flags); + } + private void updateRemoteControl() { - if (Util.isLockScreenEnabled(this)) - { - if (remoteControlClient == null) - { - - Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON); - intent.setComponent(new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName())); - remoteControlClient = new RemoteControlClient(PendingIntent.getBroadcast(this, 0, intent, 0)); - - remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY | - RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | - RemoteControlClient.FLAG_KEY_MEDIA_NEXT | - RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | - RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | - RemoteControlClient.FLAG_KEY_MEDIA_STOP); - - audioManager.registerRemoteControlClient(remoteControlClient); - } - - Log.i(TAG, String.format("In updateRemoteControl, playerState: %s [%d]", playerState, getPlayerPosition())); - - switch (playerState) - { - case STARTED: - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) - { - remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); - } - else - { - remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING, getPlayerPosition(), 1); - } - break; - case PAUSED: - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) - { - remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED); - } - else - { - remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED, getPlayerPosition(), 1); - } - break; - case DOWNLOADING: - case PREPARING: - remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_BUFFERING); - break; - case IDLE: - case COMPLETED: - case PREPARED: - case STOPPED: - remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED); - break; - default: - remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED); - break; - } - - try - { - if (currentPlaying != null) - { - Bitmap lockScreenBitmap; - if (currentSong != currentPlaying.getSong()) - { - currentSong = currentPlaying.getSong(); - lockScreenBitmap = FileUtil.getAlbumArtBitmap(this, currentSong, lockScreenBitmapSize, true); - } - else - { - return; - } - - String artist = currentSong.getArtist(); - String album = currentSong.getAlbum(); - String title = String.format("%s - %s", artist, currentSong.getTitle()); - Long duration = (long) currentSong.getDuration() * 1000; - - // Update the remote controls - remoteControlClient.editMetadata(true).putString(MediaMetadataRetriever.METADATA_KEY_TITLE, title).putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, artist).putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, album).apply(); - remoteControlClient.editMetadata(false).putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, lockScreenBitmap).apply(); - remoteControlClient.editMetadata(false).putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, duration).apply(); - } - } - catch (Exception e) - { - Log.e(TAG, "Exception in updateRemoteControl", e); - } - } - else + if (!Util.isLockScreenEnabled(this)) { if (remoteControlClient != null) { @@ -1522,7 +1482,77 @@ public class DownloadServiceImpl extends Service implements DownloadService audioManager.unregisterRemoteControlClient(remoteControlClient); remoteControlClient = null; } + + return; } + + //try + //{ + Log.i(TAG, String.format("In updateRemoteControl, playerState: %s [%d]", playerState, getPlayerPosition())); + + switch (playerState) + { + case STARTED: + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) + { + remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); + } + else + { + remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING, getPlayerPosition(), 1.0f); + } + break; + default: + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) + { + remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED); + } + else + { + remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED, getPlayerPosition(), 1.0f); + } + break; + // case DOWNLOADING: + // case PREPARING: + // remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_BUFFERING); + // break; + // case IDLE: + // case COMPLETED: + // case PREPARED: + // case STOPPED: + // remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED); + // break; + // default: + // remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED); + // break; + } + + if (currentPlaying != null) + { + MusicDirectory.Entry currentSong = currentPlaying.getSong(); + + Bitmap lockScreenBitmap = FileUtil.getAlbumArtBitmap(this, currentSong, 0, true); + + String artist = currentSong.getArtist(); + String album = currentSong.getAlbum(); + String title = currentSong.getTitle(); + Long duration = (long) currentSong.getDuration() * 1000; + + + remoteControlClient.editMetadata(true).putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, artist).putString(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST, artist).putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, album).putString(MediaMetadataRetriever.METADATA_KEY_TITLE, title).putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, duration).putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, lockScreenBitmap).apply(); + + + // Update the remote controls + //remoteControlClient.editMetadata(true).putString(MediaMetadataRetriever.METADATA_KEY_TITLE, title).putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, artist).putString(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST, artist).putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, album).apply(); + //remoteControlClient.editMetadata(false).putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, lockScreenBitmap).apply(); + //remoteControlClient.editMetadata(false).putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, duration).apply(); + } + //} + //catch (Exception e) + //{ + + //Log.e(TAG, "Exception in updateRemoteControl", e); + //} } private synchronized void bufferAndPlay() @@ -1591,7 +1621,7 @@ public class DownloadServiceImpl extends Service implements DownloadService SeekBar progressBar = DownloadActivity.getProgressBar(); MusicDirectory.Entry song = downloadFile.getSong(); - if (progressBar != null && song.getTranscodedContentType() == null && Util.getMaxBitRate(getApplicationContext()) == 0) + if (progressBar != null && song.getTranscodedContentType() == null && Util.getMaxBitRate(DownloadServiceImpl.this) == 0) { secondaryProgress = (int) (((double) percent / (double) 100) * progressBar.getMax()); progressBar.setSecondaryProgress(secondaryProgress); @@ -1621,7 +1651,7 @@ public class DownloadServiceImpl extends Service implements DownloadService if (position != 0) { Log.i(TAG, String.format("Restarting player from position %d", position)); - mediaPlayer.seekTo(position); + seekTo(position); } cachedPosition = position; @@ -1947,6 +1977,11 @@ public class DownloadServiceImpl extends Service implements DownloadService DownloadFile downloadFile = backgroundDownloadList.get(i); if (downloadFile.isWorkDone() && (!downloadFile.shouldSave() || downloadFile.isSaved())) { + if (Util.getShouldScanMedia(this)) + { + Util.scanMedia(this, downloadFile.getCompleteFile()); + } + // Don't need to keep list like active song list backgroundDownloadList.remove(i); revision++; diff --git a/src/com/thejoshwa/ultrasonic/androidapp/service/parser/IndexesParser.java b/src/com/thejoshwa/ultrasonic/androidapp/service/parser/IndexesParser.java index 31205bf1..5cd734fd 100644 --- a/src/com/thejoshwa/ultrasonic/androidapp/service/parser/IndexesParser.java +++ b/src/com/thejoshwa/ultrasonic/androidapp/service/parser/IndexesParser.java @@ -38,10 +38,12 @@ import java.util.List; public class IndexesParser extends AbstractParser { private static final String TAG = IndexesParser.class.getSimpleName(); + private Context context; public IndexesParser(Context context) { super(context); + this.context = context; } public Indexes parse(Reader reader, ProgressListener progressListener) throws Exception @@ -87,7 +89,7 @@ public class IndexesParser extends AbstractParser if (artists.size() % 10 == 0) { - String msg = getContext().getResources().getString(R.string.parser_artist_count, artists.size()); + String msg = this.context.getResources().getString(R.string.parser_artist_count, artists.size()); updateProgress(progressListener, msg); } } @@ -116,7 +118,7 @@ public class IndexesParser extends AbstractParser long t1 = System.currentTimeMillis(); Log.d(TAG, "Got " + artists.size() + " artist(s) in " + (t1 - t0) + "ms."); - String msg = getContext().getResources().getString(R.string.parser_artist_count, artists.size()); + String msg = this.context.getResources().getString(R.string.parser_artist_count, artists.size()); updateProgress(progressListener, msg); return new Indexes(lastModified == null ? 0L : lastModified, ignoredArticles, shortcuts, artists); diff --git a/src/com/thejoshwa/ultrasonic/androidapp/util/Constants.java b/src/com/thejoshwa/ultrasonic/androidapp/util/Constants.java index 8c5ebe05..15058088 100644 --- a/src/com/thejoshwa/ultrasonic/androidapp/util/Constants.java +++ b/src/com/thejoshwa/ultrasonic/androidapp/util/Constants.java @@ -127,6 +127,7 @@ public final class Constants public static final String PREFERENCES_KEY_DEFAULT_SHARE_GREETING = "sharingDefaultGreeting"; public static final String PREFERENCES_KEY_DEFAULT_SHARE_EXPIRATION = "sharingDefaultExpiration"; public static final String PREFERENCES_KEY_SHOW_ALL_SONGS_BY_ARTIST = "showAllSongsByArtist"; + public static final String PREFERENCES_KEY_SCAN_MEDIA = "scanMedia"; // Name of the preferences file. public static final String PREFERENCES_FILE_NAME = "com.thejoshwa.ultrasonic.androidapp_preferences"; diff --git a/src/com/thejoshwa/ultrasonic/androidapp/util/FileUtil.java b/src/com/thejoshwa/ultrasonic/androidapp/util/FileUtil.java index d03373f7..199557b1 100644 --- a/src/com/thejoshwa/ultrasonic/androidapp/util/FileUtil.java +++ b/src/com/thejoshwa/ultrasonic/androidapp/util/FileUtil.java @@ -24,6 +24,7 @@ import android.graphics.BitmapFactory; import android.os.Environment; import android.util.Log; +import com.thejoshwa.ultrasonic.androidapp.activity.SubsonicTabActivity; import com.thejoshwa.ultrasonic.androidapp.domain.Artist; import com.thejoshwa.ultrasonic.androidapp.domain.MusicDirectory; @@ -125,8 +126,17 @@ public class FileUtil public static Bitmap getAlbumArtBitmap(Context context, MusicDirectory.Entry entry, int size, boolean highQuality) { + if (entry == null) return null; + File albumArtFile = getAlbumArtFile(context, entry); + Bitmap bitmap = SubsonicTabActivity.getInstance().getImageLoader().getImageBitmap(entry, true, size); + + if (bitmap != null) + { + return bitmap.copy(bitmap.getConfig(), false); + } + if (albumArtFile != null && albumArtFile.exists()) { final BitmapFactory.Options opt = new BitmapFactory.Options(); @@ -147,9 +157,14 @@ public class FileUtil opt.inJustDecodeBounds = false; } - Bitmap bitmap = BitmapFactory.decodeFile(albumArtFile.getPath(), opt); + bitmap = BitmapFactory.decodeFile(albumArtFile.getPath(), opt); Log.i("getAlbumArtBitmap", String.valueOf(size)); + if (bitmap != null) + { + SubsonicTabActivity.getInstance().getImageLoader().addImageToCache(bitmap, entry, size); + } + return bitmap == null ? null : bitmap; } diff --git a/src/com/thejoshwa/ultrasonic/androidapp/util/ImageLoader.java b/src/com/thejoshwa/ultrasonic/androidapp/util/ImageLoader.java index f49ff19d..56585e48 100644 --- a/src/com/thejoshwa/ultrasonic/androidapp/util/ImageLoader.java +++ b/src/com/thejoshwa/ultrasonic/androidapp/util/ImageLoader.java @@ -131,7 +131,37 @@ public class ImageLoader implements Runnable private static String getKey(String coverArtId, int size) { - return coverArtId + size; + return coverArtId + ":" + size; + } + + public Bitmap getImageBitmap(MusicDirectory.Entry entry, boolean large, int size) + { + if (entry == null) + { + return null; + } + + String coverArt = entry.getCoverArt(); + + if (coverArt == null) + { + return null; + } + + if (size <= 0) + { + size = large ? imageSizeLarge : imageSizeDefault; + } + + Bitmap bitmap = cache.get(getKey(coverArt, size)); + + if (bitmap != null) + { + Bitmap.Config config = bitmap.getConfig(); + return bitmap.copy(config, false); + } + + return null; } private void setImageBitmap(View view, Bitmap bitmap, boolean crossFade) @@ -183,6 +213,11 @@ public class ImageLoader implements Runnable } } + public void addImageToCache(Bitmap bitmap, MusicDirectory.Entry entry, int size) + { + cache.put(getKey(entry.getCoverArt(), size), bitmap); + } + public void clear() { queue.clear(); @@ -233,7 +268,7 @@ public class ImageLoader implements Runnable MusicService musicService = MusicServiceFactory.getMusicService(view.getContext()); final Bitmap bitmap = musicService.getCoverArt(view.getContext(), entry, size, saveToFile, highQuality, null); - cache.put(getKey(entry.getCoverArt(), size), bitmap); + addImageToCache(bitmap, entry, size); handler.post(new Runnable() { diff --git a/src/com/thejoshwa/ultrasonic/androidapp/util/Util.java b/src/com/thejoshwa/ultrasonic/androidapp/util/Util.java index 6d68d4f0..4bc0ea1a 100644 --- a/src/com/thejoshwa/ultrasonic/androidapp/util/Util.java +++ b/src/com/thejoshwa/ultrasonic/androidapp/util/Util.java @@ -37,6 +37,7 @@ import android.media.AudioManager; import android.media.AudioManager.OnAudioFocusChangeListener; import android.net.ConnectivityManager; import android.net.NetworkInfo; +import android.net.Uri; import android.net.wifi.WifiManager; import android.os.Environment; import android.os.Parcelable; @@ -1611,4 +1612,17 @@ public class Util extends DownloadActivity SharedPreferences preferences = getPreferences(context); return preferences.getBoolean(Constants.PREFERENCES_KEY_SHOW_ALL_SONGS_BY_ARTIST, false); } + + public static boolean getShouldScanMedia(Context context) + { + SharedPreferences preferences = getPreferences(context); + return preferences.getBoolean(Constants.PREFERENCES_KEY_SCAN_MEDIA, false); + } + + public static void scanMedia(Context context, File file) + { + Uri uri = Uri.fromFile(file); + Intent scanFileIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri); + context.sendBroadcast(scanFileIntent); + } }