From a8ba29fe0bd4dafe1498d9f3c794907b5f2ab1a9 Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Sun, 13 Dec 2015 13:25:49 +0100 Subject: [PATCH 1/4] Try to lower memory usage --- .../activity/MediaplayerActivity.java | 18 ++++++++++++++++-- .../main/res/layout/new_episodes_listitem.xml | 4 ++-- app/src/main/res/layout/queue_listitem.xml | 4 ++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java index c53a5257b..febaa68e5 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java @@ -1,10 +1,12 @@ package de.danoeh.antennapod.activity; +import android.annotation.TargetApi; import android.content.DialogInterface; import android.content.Intent; import android.graphics.PixelFormat; import android.media.AudioManager; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.AlertDialog; @@ -12,7 +14,6 @@ import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; - import android.view.View; import android.widget.ImageButton; import android.widget.SeekBar; @@ -20,9 +21,9 @@ import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; import com.afollestad.materialdialogs.MaterialDialog; +import com.bumptech.glide.Glide; import de.danoeh.antennapod.R; -import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.service.playback.PlaybackService; @@ -239,6 +240,19 @@ public abstract class MediaplayerActivity extends ActionBarActivity Log.d(TAG, "onDestroy()"); } + @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) + @Override + public void onTrimMemory(int level) { + super.onTrimMemory(level); + Glide.get(this).trimMemory(level); + } + + @Override + public void onLowMemory() { + super.onLowMemory(); + Glide.get(this).clearMemory(); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); diff --git a/app/src/main/res/layout/new_episodes_listitem.xml b/app/src/main/res/layout/new_episodes_listitem.xml index cde1b4aa6..0f1e873f3 100644 --- a/app/src/main/res/layout/new_episodes_listitem.xml +++ b/app/src/main/res/layout/new_episodes_listitem.xml @@ -33,8 +33,8 @@ Date: Sun, 13 Dec 2015 18:33:44 +0100 Subject: [PATCH 2/4] Load bitmaps in the size we need them --- .../service/playback/PlaybackService.java | 14 ++++------- .../playback/PlaybackServiceMediaPlayer.java | 24 ++++++++++--------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index 1313d9607..2214e992f 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -30,7 +30,6 @@ import android.widget.Toast; import com.bumptech.glide.Glide; import java.util.List; -import java.util.concurrent.ExecutionException; import de.danoeh.antennapod.core.ClientConfig; import de.danoeh.antennapod.core.R; @@ -808,14 +807,11 @@ public class PlaybackService extends Service { .load(info.playable.getImageUri()) .asBitmap() .diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY) - .into(-1, -1) // this resizing would not be exact, so we have - // scale the bitmap ourselves + .centerCrop() + .into(iconSize, iconSize) .get(); - icon = Bitmap.createScaledBitmap(icon, iconSize, iconSize, true); - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (ExecutionException e) { - e.printStackTrace(); + } catch(Exception e) { + Log.e(TAG, Log.getStackTraceString(e)); } } } @@ -835,7 +831,7 @@ public class PlaybackService extends Service { String contentTitle = info.playable.getFeedTitle(); Notification notification = null; - NotificationCompat.Builder notificationBuilder = new android.support.v7.app.NotificationCompat.Builder( + NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( PlaybackService.this) .setContentTitle(contentTitle) .setContentText(contentText) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java index 6ad376bf0..56b81ec4a 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java @@ -17,12 +17,13 @@ import android.support.v4.media.session.PlaybackStateCompat; import android.telephony.TelephonyManager; import android.util.Log; import android.util.Pair; +import android.view.Display; import android.view.InputDevice; import android.view.KeyEvent; import android.view.SurfaceHolder; +import android.view.WindowManager; import com.bumptech.glide.Glide; -import com.bumptech.glide.request.target.Target; import java.io.IOException; import java.util.concurrent.CountDownLatch; @@ -292,20 +293,21 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, p.getDuration()); builder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, p.getEpisodeTitle()); builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, p.getFeedTitle()); - if (p.getImageUri() != null) { - if (UserPreferences.setLockscreenBackground()) { - builder.putString(MediaMetadataCompat.METADATA_KEY_ART_URI, p.getImageUri().toString()); - try { - Bitmap art = Glide.with(context) + if (p.getImageUri() != null && UserPreferences.setLockscreenBackground()) { + builder.putString(MediaMetadataCompat.METADATA_KEY_ART_URI, p.getImageUri().toString()); + try { + WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + Display display = wm.getDefaultDisplay(); + Bitmap art = Glide.with(context) .load(p.getImageUri()) .asBitmap() .diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY) - .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) + .centerCrop() + .into(display.getWidth(), display.getHeight()) .get(); - builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, art); - } catch (Exception e) { - Log.e(TAG, Log.getStackTraceString(e)); - } + builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, art); + } catch (Exception e) { + Log.e(TAG, Log.getStackTraceString(e)); } } mediaSession.setMetadata(builder.build()); From 3997982e26332f6211dcaf89f399ab583af7cff5 Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Sun, 13 Dec 2015 22:23:14 +0100 Subject: [PATCH 3/4] Catch throwable --- .../antennapod/core/service/playback/PlaybackService.java | 4 ++-- .../core/service/playback/PlaybackServiceMediaPlayer.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index 2214e992f..6e67d1a89 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -810,8 +810,8 @@ public class PlaybackService extends Service { .centerCrop() .into(iconSize, iconSize) .get(); - } catch(Exception e) { - Log.e(TAG, Log.getStackTraceString(e)); + } catch(Throwable tr) { + Log.e(TAG, Log.getStackTraceString(tr)); } } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java index 56b81ec4a..7fdf443c2 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java @@ -306,8 +306,8 @@ public class PlaybackServiceMediaPlayer implements SharedPreferences.OnSharedPre .into(display.getWidth(), display.getHeight()) .get(); builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, art); - } catch (Exception e) { - Log.e(TAG, Log.getStackTraceString(e)); + } catch (Throwable tr) { + Log.e(TAG, Log.getStackTraceString(tr)); } } mediaSession.setMetadata(builder.build()); From 32648ba17f3a2503e87584493ad3dc4188dd85c8 Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Mon, 14 Dec 2015 00:45:57 +0100 Subject: [PATCH 4/4] *_* --- .../antennapod/core/service/playback/PlaybackService.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index 6e67d1a89..129055f92 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -19,7 +19,7 @@ import android.os.Build; import android.os.IBinder; import android.os.Vibrator; import android.preference.PreferenceManager; -import android.support.v4.app.NotificationCompat; +import android.support.v7.app.NotificationCompat; import android.text.TextUtils; import android.util.Log; import android.util.Pair; @@ -831,7 +831,8 @@ public class PlaybackService extends Service { String contentTitle = info.playable.getFeedTitle(); Notification notification = null; - NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( + // Builder is v7, even if some not overwritten methods return its parent's v4 interface + NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder( PlaybackService.this) .setContentTitle(contentTitle) .setContentText(contentText)