From 15142c1ec380cad850494f04fb1e45a20b9dcbc4 Mon Sep 17 00:00:00 2001 From: Vasiliy Date: Sun, 24 Feb 2019 10:51:30 +0200 Subject: [PATCH] Fix AudioManager memory leak --- .../newpipe/player/AudioServiceLeakFix.java | 30 +++++++++++++++++++ .../newpipe/player/BackgroundPlayer.java | 5 ++++ .../newpipe/player/MainVideoPlayer.java | 5 ++++ .../newpipe/player/PopupVideoPlayer.java | 5 ++++ 4 files changed, 45 insertions(+) create mode 100644 app/src/main/java/org/schabi/newpipe/player/AudioServiceLeakFix.java diff --git a/app/src/main/java/org/schabi/newpipe/player/AudioServiceLeakFix.java b/app/src/main/java/org/schabi/newpipe/player/AudioServiceLeakFix.java new file mode 100644 index 000000000..9f0c849f5 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/player/AudioServiceLeakFix.java @@ -0,0 +1,30 @@ +package org.schabi.newpipe.player; + +import android.content.Context; +import android.content.ContextWrapper; + +/** + * Fixes a leak caused by AudioManager using an Activity context. + * Tracked at https://android-review.googlesource.com/#/c/140481/1 and + * https://github.com/square/leakcanary/issues/205 + * Source: + * https://gist.github.com/jankovd/891d96f476f7a9ce24e2 + */ +public class AudioServiceLeakFix extends ContextWrapper { + + AudioServiceLeakFix(Context base) { + super(base); + } + + public static ContextWrapper preventLeakOf(Context base) { + return new AudioServiceLeakFix(base); + } + + @Override + public Object getSystemService(String name) { + if (Context.AUDIO_SERVICE.equals(name)) { + return getApplicationContext().getSystemService(name); + } + return super.getSystemService(name); + } +} \ No newline at end of file diff --git a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java index 91faa1014..94b401f09 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java @@ -130,6 +130,11 @@ public final class BackgroundPlayer extends Service { onClose(); } + @Override + protected void attachBaseContext(Context base) { + super.attachBaseContext(AudioServiceLeakFix.preventLeakOf(base)); + } + @Override public IBinder onBind(Intent intent) { return mBinder; diff --git a/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java index f4fea5165..5dd931b54 100644 --- a/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java @@ -241,6 +241,11 @@ public final class MainVideoPlayer extends AppCompatActivity isBackPressed = false; } + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(AudioServiceLeakFix.preventLeakOf(newBase)); + } + /*////////////////////////////////////////////////////////////////////////// // State Saving //////////////////////////////////////////////////////////////////////////*/ diff --git a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java index 8ea3d509c..7578c444c 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java @@ -181,6 +181,11 @@ public final class PopupVideoPlayer extends Service { closePopup(); } + @Override + protected void attachBaseContext(Context base) { + super.attachBaseContext(AudioServiceLeakFix.preventLeakOf(base)); + } + @Override public IBinder onBind(Intent intent) { return mBinder;