diff options
author | Scott Jackson <daneren2005@gmail.com> | 2015-11-07 12:37:53 -0800 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2015-11-07 12:37:53 -0800 |
commit | 2fa4595ea11e60df6699654e8656b11adf604847 (patch) | |
tree | d31910a1c98c069b22796ab9da3449fd6570cbd2 /app/src | |
parent | f0be095e584d7391ef5c83779b6fe7410109cac9 (diff) | |
download | dsub-2fa4595ea11e60df6699654e8656b11adf604847.tar.gz dsub-2fa4595ea11e60df6699654e8656b11adf604847.tar.bz2 dsub-2fa4595ea11e60df6699654e8656b11adf604847.zip |
#296 Respond to service low memory warnings
Diffstat (limited to 'app/src')
8 files changed, 70 insertions, 1 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java b/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java index 7620be48..7275e447 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java @@ -31,6 +31,7 @@ import static github.daneren2005.dsub.domain.PlayerState.STOPPED; import static github.daneren2005.dsub.domain.RemoteControlState.LOCAL; import github.daneren2005.dsub.R; +import github.daneren2005.dsub.activity.SubsonicActivity; import github.daneren2005.dsub.audiofx.AudioEffectsController; import github.daneren2005.dsub.audiofx.EqualizerController; import github.daneren2005.dsub.domain.Bookmark; @@ -42,6 +43,7 @@ import github.daneren2005.dsub.domain.RepeatMode; import github.daneren2005.dsub.domain.ServerInfo; import github.daneren2005.dsub.receiver.MediaButtonIntentReceiver; import github.daneren2005.dsub.util.ArtistRadioBuffer; +import github.daneren2005.dsub.util.ImageLoader; import github.daneren2005.dsub.util.Notifications; import github.daneren2005.dsub.util.SilentBackgroundTask; import github.daneren2005.dsub.util.Constants; @@ -67,6 +69,7 @@ import java.util.TimerTask; import android.annotation.TargetApi; import android.app.Service; +import android.content.ComponentCallbacks2; import android.content.ComponentName; import android.content.Context; import android.content.Intent; @@ -279,6 +282,25 @@ public class DownloadService extends Service { } @Override + public void onTrimMemory(int level) { + Log.w(TAG, "Level: " + level); + ImageLoader imageLoader = SubsonicActivity.getStaticImageLoader(this); + if(imageLoader != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + if (level < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) { + if (level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) { + imageLoader.onLowMemory(0.75f); + } else if (level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW) { + imageLoader.onLowMemory(0.50f); + } else if(level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE) { + imageLoader.onLowMemory(0.25f); + } + } else if (level >= TRIM_MEMORY_MODERATE) { + imageLoader.onLowMemory(0.25f); + } + } + } + + @Override public void onDestroy() { super.onDestroy(); instance = null; diff --git a/app/src/main/java/github/daneren2005/dsub/util/ImageLoader.java b/app/src/main/java/github/daneren2005/dsub/util/ImageLoader.java index 06295382..7ae1648f 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/ImageLoader.java +++ b/app/src/main/java/github/daneren2005/dsub/util/ImageLoader.java @@ -68,6 +68,7 @@ public class ImageLoader { private final int imageSizeLarge; private final int avatarSizeDefault; private boolean clearingCache = false; + private final int cacheSize; private final static int[] COLORS = {0xFF33B5E5, 0xFFAA66CC, 0xFF99CC00, 0xFFFFBB33, 0xFFFF4444}; @@ -75,7 +76,7 @@ public class ImageLoader { this.context = context; handler = new Handler(Looper.getMainLooper()); final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); - final int cacheSize = maxMemory / 4; + cacheSize = maxMemory / 4; // Determine the density-dependent image sizes. imageSizeDefault = context.getResources().getDrawable(R.drawable.unknown_album).getIntrinsicHeight(); @@ -114,6 +115,9 @@ public class ImageLoader { } }.execute(); } + public void onLowMemory(float percent) { + cache.trimToSize(Math.round(cacheSize * percent)); + } private Bitmap getUnknownImage(MusicDirectory.Entry entry, int size) { String key; diff --git a/app/src/main/java/github/daneren2005/dsub/view/AlbumView.java b/app/src/main/java/github/daneren2005/dsub/view/AlbumView.java index 343a36f7..048d5a75 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/AlbumView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/AlbumView.java @@ -86,6 +86,10 @@ public class AlbumView extends UpdateView2<MusicDirectory.Entry, ImageLoader> { file = null; } + public void onUpdateImageView() { + imageTask = item2.loadImage(coverArtView, item, false, true); + } + @Override protected void updateBackground() { if(file == null) { diff --git a/app/src/main/java/github/daneren2005/dsub/view/PlaylistView.java b/app/src/main/java/github/daneren2005/dsub/view/PlaylistView.java index 7d475262..7b5750a8 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/PlaylistView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/PlaylistView.java @@ -55,6 +55,10 @@ public class PlaylistView extends UpdateView<Playlist> { imageTask = imageLoader.loadImage(coverArtView, playlist, false, true); } + public void onUpdateImageView() { + imageTask = imageLoader.loadImage(coverArtView, item, false, true); + } + @Override protected void updateBackground() { pinned = SyncUtil.isSyncedPlaylist(context, item.getId()); diff --git a/app/src/main/java/github/daneren2005/dsub/view/PodcastChannelView.java b/app/src/main/java/github/daneren2005/dsub/view/PodcastChannelView.java index 4b19eedd..d3f50954 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/PodcastChannelView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/PodcastChannelView.java @@ -78,6 +78,12 @@ public class PodcastChannelView extends UpdateView<PodcastChannel> { imageTask = imageLoader.loadImage(coverArtView, channel, false, true); } } + + public void onUpdateImageView() { + if(imageLoader != null) { + imageTask = imageLoader.loadImage(coverArtView, item, false, true); + } + } @Override protected void updateBackground() { diff --git a/app/src/main/java/github/daneren2005/dsub/view/RecyclingImageView.java b/app/src/main/java/github/daneren2005/dsub/view/RecyclingImageView.java index 0c85697f..a7208cee 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/RecyclingImageView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/RecyclingImageView.java @@ -27,6 +27,8 @@ import android.util.AttributeSet; import android.widget.ImageView; public class RecyclingImageView extends ImageView { + private boolean invalidated = false; + public RecyclingImageView(Context context) { super(context); } @@ -51,6 +53,7 @@ public class RecyclingImageView extends ImageView { if(drawable instanceof BitmapDrawable) { if (isBitmapRecycled(drawable)) { this.setImageDrawable(null); + invalidated = true; } } else if(drawable instanceof TransitionDrawable) { TransitionDrawable transitionDrawable = (TransitionDrawable) drawable; @@ -59,6 +62,7 @@ public class RecyclingImageView extends ImageView { Drawable lastDrawable = transitionDrawable.getDrawable(transitionDrawable.getNumberOfLayers() - 1); if(isBitmapRecycled(lastDrawable)) { this.setImageDrawable(null); + invalidated = true; } else { // Go through earlier bitmaps and make sure that they are not recycled for (int i = 0; i < transitionDrawable.getNumberOfLayers(); i++) { @@ -76,6 +80,12 @@ public class RecyclingImageView extends ImageView { super.onDraw(canvas); } + @Override + public void setImageDrawable(Drawable drawable) { + super.setImageDrawable(drawable); + invalidated = false; + } + private boolean isBitmapRecycled(Drawable drawable) { if(!(drawable instanceof BitmapDrawable)) { return false; @@ -88,4 +98,8 @@ public class RecyclingImageView extends ImageView { return false; } } + + public boolean isInvalidated() { + return invalidated; + } } diff --git a/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java b/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java index d59f23d0..8f3b5271 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java @@ -275,6 +275,13 @@ public abstract class UpdateView<T> extends LinearLayout { ratingBar.setRating(isRated); rating = isRated; } + + if(coverArtView != null && coverArtView instanceof RecyclingImageView) { + RecyclingImageView recyclingImageView = (RecyclingImageView) coverArtView; + if(recyclingImageView.isInvalidated()) { + onUpdateImageView(); + } + } } public boolean isCheckable() { @@ -295,6 +302,10 @@ public abstract class UpdateView<T> extends LinearLayout { } + public void onUpdateImageView() { + + } + public static class UpdateViewHolder<T> extends RecyclerView.ViewHolder { private UpdateView updateView; private View view; diff --git a/app/src/main/java/github/daneren2005/dsub/view/UserView.java b/app/src/main/java/github/daneren2005/dsub/view/UserView.java index a97d755b..38ad4f78 100644 --- a/app/src/main/java/github/daneren2005/dsub/view/UserView.java +++ b/app/src/main/java/github/daneren2005/dsub/view/UserView.java @@ -47,4 +47,8 @@ public class UserView extends UpdateView2<User, ImageLoader> { usernameView.setText(user.getUsername()); imageTask = imageLoader.loadAvatar(context, avatarView, user.getUsername()); } + + public void onUpdateImageView() { + imageTask = item2.loadAvatar(context, avatarView, item.getUsername()); + } } |