From 402f0b15aacc7a3d795bd4d4b33a8e3a082b31a0 Mon Sep 17 00:00:00 2001 From: Thompson3142 Date: Tue, 3 Dec 2024 00:51:15 +0100 Subject: [PATCH 1/7] Initial commit for better handling of background crashes --- app/build.gradle | 1 + .../newpipe/error/AppLifecycleObserver.kt | 32 +++++++++++++++++++ .../org/schabi/newpipe/error/ErrorUtil.kt | 9 +++++- .../newpipe/fragments/MainFragment.java | 3 ++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt diff --git a/app/build.gradle b/app/build.gradle index 662437f642a..c082ef06e2d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -227,6 +227,7 @@ dependencies { implementation 'androidx.fragment:fragment-ktx:1.6.2' implementation "androidx.lifecycle:lifecycle-livedata-ktx:${androidxLifecycleVersion}" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${androidxLifecycleVersion}" + implementation "androidx.lifecycle:lifecycle-process:${androidxLifecycleVersion}" implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.1.0' implementation 'androidx.media:media:1.7.0' implementation 'androidx.preference:preference:1.2.1' diff --git a/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt b/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt new file mode 100644 index 00000000000..5dfc62c63f9 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt @@ -0,0 +1,32 @@ +package org.schabi.newpipe.error + +import android.util.Log +import androidx.lifecycle.DefaultLifecycleObserver +import androidx.lifecycle.LifecycleOwner +import java.util.concurrent.atomic.AtomicLong + +object AppLifecycleObserver : DefaultLifecycleObserver { + private var isInBackground = false + private val lastBackgroundTimestamp = AtomicLong(0) + private var TAG = javaClass.simpleName + + override fun onStart(owner: LifecycleOwner) { + isInBackground = false + Log.d(TAG, "App moved to foreground") + } + + override fun onStop(owner: LifecycleOwner) { + isInBackground = true + lastBackgroundTimestamp.set(System.currentTimeMillis()) + Log.d(TAG, "App moved to background") + } + + fun isAppInBackground(): Boolean = isInBackground + + /** + * @return the elapsed time since the app moved to the background or 0 if it is in foreground + */ + fun getTimeSinceLastBackground(): Long { + return if (isInBackground) System.currentTimeMillis() - lastBackgroundTimestamp.get() else 0 + } +} diff --git a/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt b/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt index dcbc1141335..3fa23aa8a90 100644 --- a/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt +++ b/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt @@ -35,12 +35,19 @@ class ErrorUtil { * activity (since the workflow would be interrupted anyway in that case). So never use this * for background services. * + * If this method is called while the app has been in the background for more than + * 10 seconds it will not start an error activity and instead create a notification + * * @param context the context to use to start the new activity * @param errorInfo the error info to be reported */ @JvmStatic fun openActivity(context: Context, errorInfo: ErrorInfo) { - context.startActivity(getErrorActivityIntent(context, errorInfo)) + if (AppLifecycleObserver.getTimeSinceLastBackground() > 10000) { + createNotification(context, errorInfo) + } else { + context.startActivity(getErrorActivityIntent(context, errorInfo)) + } } /** diff --git a/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java index 381de50032a..48087e91102 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java @@ -28,6 +28,7 @@ import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentStatePagerAdapterMenuWorkaround; +import androidx.lifecycle.ProcessLifecycleOwner; import androidx.preference.PreferenceManager; import androidx.viewpager.widget.ViewPager; @@ -36,6 +37,7 @@ import org.schabi.newpipe.BaseFragment; import org.schabi.newpipe.R; import org.schabi.newpipe.databinding.FragmentMainBinding; +import org.schabi.newpipe.error.AppLifecycleObserver; import org.schabi.newpipe.error.ErrorUtil; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.local.playlist.LocalPlaylistFragment; @@ -71,6 +73,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); + ProcessLifecycleOwner.get().getLifecycle().addObserver(AppLifecycleObserver.INSTANCE); setHasOptionsMenu(true); tabsManager = TabsManager.getManager(activity); tabsManager.setSavedTabsListener(() -> { From bdbdc29494ac9b3b90a28ac3784a11e8e203cde4 Mon Sep 17 00:00:00 2001 From: Thompson3142 Date: Tue, 3 Dec 2024 23:18:33 +0100 Subject: [PATCH 2/7] Fix crashing behaviour with entry in SharedPreferences --- app/src/main/java/org/schabi/newpipe/App.java | 5 +++ .../newpipe/error/AppLifecycleObserver.kt | 31 +++++++++++-------- .../org/schabi/newpipe/error/ErrorUtil.kt | 2 +- .../newpipe/fragments/MainFragment.java | 3 -- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/App.java b/app/src/main/java/org/schabi/newpipe/App.java index 9bc25d55d7f..5338b43b503 100644 --- a/app/src/main/java/org/schabi/newpipe/App.java +++ b/app/src/main/java/org/schabi/newpipe/App.java @@ -8,12 +8,14 @@ import androidx.annotation.NonNull; import androidx.core.app.NotificationChannelCompat; import androidx.core.app.NotificationManagerCompat; +import androidx.lifecycle.ProcessLifecycleOwner; import androidx.preference.PreferenceManager; import com.jakewharton.processphoenix.ProcessPhoenix; import org.acra.ACRA; import org.acra.config.CoreConfigurationBuilder; +import org.schabi.newpipe.error.AppLifecycleObserver; import org.schabi.newpipe.error.ReCaptchaActivity; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.downloader.Downloader; @@ -94,6 +96,9 @@ public void onCreate() { .getInt(getString(R.string.last_used_preferences_version), -1); isFirstRun = lastUsedPrefVersion == -1; + AppLifecycleObserver.INSTANCE.initialize(this); + ProcessLifecycleOwner.get().getLifecycle().addObserver(AppLifecycleObserver.INSTANCE); + // Initialize settings first because other initializations can use its values NewPipeSettings.initSettings(this); diff --git a/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt b/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt index 5dfc62c63f9..02b72fba22f 100644 --- a/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt +++ b/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt @@ -1,32 +1,37 @@ package org.schabi.newpipe.error +import android.content.Context +import android.content.SharedPreferences import android.util.Log import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.LifecycleOwner -import java.util.concurrent.atomic.AtomicLong +import androidx.preference.PreferenceManager object AppLifecycleObserver : DefaultLifecycleObserver { - private var isInBackground = false - private val lastBackgroundTimestamp = AtomicLong(0) + private const val KEY_IS_IN_BACKGROUND = "is_in_background" private var TAG = javaClass.simpleName + private lateinit var sharedPreferences: SharedPreferences + + fun initialize(context: Context) { + sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) + } override fun onStart(owner: LifecycleOwner) { - isInBackground = false + sharedPreferences.edit().putBoolean(KEY_IS_IN_BACKGROUND, false).apply() Log.d(TAG, "App moved to foreground") } override fun onStop(owner: LifecycleOwner) { - isInBackground = true - lastBackgroundTimestamp.set(System.currentTimeMillis()) + sharedPreferences.edit().putBoolean(KEY_IS_IN_BACKGROUND, true).apply() Log.d(TAG, "App moved to background") } - fun isAppInBackground(): Boolean = isInBackground - - /** - * @return the elapsed time since the app moved to the background or 0 if it is in foreground - */ - fun getTimeSinceLastBackground(): Long { - return if (isInBackground) System.currentTimeMillis() - lastBackgroundTimestamp.get() else 0 + fun isInBackground(): Boolean { + Log.d( + TAG, + "Is in background? -" + + sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true) + ) + return sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true) } } diff --git a/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt b/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt index 3fa23aa8a90..5333245d2db 100644 --- a/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt +++ b/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt @@ -43,7 +43,7 @@ class ErrorUtil { */ @JvmStatic fun openActivity(context: Context, errorInfo: ErrorInfo) { - if (AppLifecycleObserver.getTimeSinceLastBackground() > 10000) { + if (AppLifecycleObserver.isInBackground()) { createNotification(context, errorInfo) } else { context.startActivity(getErrorActivityIntent(context, errorInfo)) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java index 48087e91102..381de50032a 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java @@ -28,7 +28,6 @@ import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentStatePagerAdapterMenuWorkaround; -import androidx.lifecycle.ProcessLifecycleOwner; import androidx.preference.PreferenceManager; import androidx.viewpager.widget.ViewPager; @@ -37,7 +36,6 @@ import org.schabi.newpipe.BaseFragment; import org.schabi.newpipe.R; import org.schabi.newpipe.databinding.FragmentMainBinding; -import org.schabi.newpipe.error.AppLifecycleObserver; import org.schabi.newpipe.error.ErrorUtil; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.local.playlist.LocalPlaylistFragment; @@ -73,7 +71,6 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); - ProcessLifecycleOwner.get().getLifecycle().addObserver(AppLifecycleObserver.INSTANCE); setHasOptionsMenu(true); tabsManager = TabsManager.getManager(activity); tabsManager.setSavedTabsListener(() -> { From 9ebae13a4314772e10775dcdbfdae415a83134ad Mon Sep 17 00:00:00 2001 From: Thompson3142 Date: Thu, 12 Dec 2024 22:48:33 +0100 Subject: [PATCH 3/7] A few minor improvements --- app/src/main/java/org/schabi/newpipe/App.java | 6 ++--- .../newpipe/error/AppLifecycleObserver.kt | 22 +++++++++---------- .../org/schabi/newpipe/error/ErrorUtil.kt | 7 +++--- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/App.java b/app/src/main/java/org/schabi/newpipe/App.java index 5338b43b503..39f21504b6e 100644 --- a/app/src/main/java/org/schabi/newpipe/App.java +++ b/app/src/main/java/org/schabi/newpipe/App.java @@ -81,6 +81,9 @@ protected void attachBaseContext(final Context base) { @Override public void onCreate() { super.onCreate(); + // Initialize the AppLifecycleObserver + AppLifecycleObserver.INSTANCE.initialize(this); + ProcessLifecycleOwner.get().getLifecycle().addObserver(AppLifecycleObserver.INSTANCE); app = this; @@ -96,9 +99,6 @@ public void onCreate() { .getInt(getString(R.string.last_used_preferences_version), -1); isFirstRun = lastUsedPrefVersion == -1; - AppLifecycleObserver.INSTANCE.initialize(this); - ProcessLifecycleOwner.get().getLifecycle().addObserver(AppLifecycleObserver.INSTANCE); - // Initialize settings first because other initializations can use its values NewPipeSettings.initSettings(this); diff --git a/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt b/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt index 02b72fba22f..1d5e848649c 100644 --- a/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt +++ b/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt @@ -11,27 +11,27 @@ object AppLifecycleObserver : DefaultLifecycleObserver { private const val KEY_IS_IN_BACKGROUND = "is_in_background" private var TAG = javaClass.simpleName private lateinit var sharedPreferences: SharedPreferences + private lateinit var editor: SharedPreferences.Editor + // Only call this once on startup fun initialize(context: Context) { - sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) + if (!this::sharedPreferences.isInitialized) { + sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) + editor = sharedPreferences.edit() + } } override fun onStart(owner: LifecycleOwner) { - sharedPreferences.edit().putBoolean(KEY_IS_IN_BACKGROUND, false).apply() - Log.d(TAG, "App moved to foreground") + editor.putBoolean(KEY_IS_IN_BACKGROUND, false).commit() + Log.d(TAG, "App moved to foreground: ") } - override fun onStop(owner: LifecycleOwner) { - sharedPreferences.edit().putBoolean(KEY_IS_IN_BACKGROUND, true).apply() - Log.d(TAG, "App moved to background") + override fun onPause(owner: LifecycleOwner) { + editor.putBoolean(KEY_IS_IN_BACKGROUND, true).commit() + Log.d(TAG, "App moved to background: ") } fun isInBackground(): Boolean { - Log.d( - TAG, - "Is in background? -" + - sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true) - ) return sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true) } } diff --git a/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt b/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt index 5333245d2db..5704f586399 100644 --- a/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt +++ b/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt @@ -13,6 +13,7 @@ import androidx.core.app.PendingIntentCompat import androidx.fragment.app.Fragment import com.google.android.material.snackbar.Snackbar import org.schabi.newpipe.R +import org.schabi.newpipe.error.AppLifecycleObserver.isInBackground /** * This class contains all of the methods that should be used to let the user know that an error has @@ -35,15 +36,15 @@ class ErrorUtil { * activity (since the workflow would be interrupted anyway in that case). So never use this * for background services. * - * If this method is called while the app has been in the background for more than - * 10 seconds it will not start an error activity and instead create a notification + * If this method is called was called while the app was in the background previously open + * a notification instead * * @param context the context to use to start the new activity * @param errorInfo the error info to be reported */ @JvmStatic fun openActivity(context: Context, errorInfo: ErrorInfo) { - if (AppLifecycleObserver.isInBackground()) { + if (isInBackground()) { createNotification(context, errorInfo) } else { context.startActivity(getErrorActivityIntent(context, errorInfo)) From 0f0d610465af1350bae931611d4838a4ba24ebf7 Mon Sep 17 00:00:00 2001 From: Thompson3142 Date: Thu, 12 Dec 2024 23:06:13 +0100 Subject: [PATCH 4/7] Added docs for isInBackground --- .../java/org/schabi/newpipe/error/AppLifecycleObserver.kt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt b/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt index 1d5e848649c..25f63a43769 100644 --- a/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt +++ b/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt @@ -31,6 +31,11 @@ object AppLifecycleObserver : DefaultLifecycleObserver { Log.d(TAG, "App moved to background: ") } + + /** + * Returns if the app is currently in the background + * or in case of a crash the state when the crash happened + */ fun isInBackground(): Boolean { return sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true) } From 2cf584b74b9db64268232f1ef2d51518ba5d7a80 Mon Sep 17 00:00:00 2001 From: Thompson3142 Date: Mon, 16 Dec 2024 20:26:01 +0100 Subject: [PATCH 5/7] Some more minor changes --- .../java/org/schabi/newpipe/error/AppLifecycleObserver.kt | 4 ++-- app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt b/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt index 25f63a43769..7a3d9b857e2 100644 --- a/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt +++ b/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt @@ -23,12 +23,12 @@ object AppLifecycleObserver : DefaultLifecycleObserver { override fun onStart(owner: LifecycleOwner) { editor.putBoolean(KEY_IS_IN_BACKGROUND, false).commit() - Log.d(TAG, "App moved to foreground: ") + Log.d(TAG, "App moved to foreground") } override fun onPause(owner: LifecycleOwner) { editor.putBoolean(KEY_IS_IN_BACKGROUND, true).commit() - Log.d(TAG, "App moved to background: ") + Log.d(TAG, "App moved to background") } diff --git a/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt b/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt index 5704f586399..f89cac347d2 100644 --- a/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt +++ b/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt @@ -36,8 +36,7 @@ class ErrorUtil { * activity (since the workflow would be interrupted anyway in that case). So never use this * for background services. * - * If this method is called was called while the app was in the background previously open - * a notification instead + * If the crashed while the app was in the background open a notification instead * * @param context the context to use to start the new activity * @param errorInfo the error info to be reported From 09e2f8f71736a119c0bda772fbecab66f69c5f7f Mon Sep 17 00:00:00 2001 From: Thompson3142 Date: Tue, 17 Dec 2024 19:41:59 +0100 Subject: [PATCH 6/7] Overwrite methods in MainActivity instead of creating a new class --- app/src/main/java/org/schabi/newpipe/App.java | 5 --- .../java/org/schabi/newpipe/MainActivity.java | 30 +++++++++---- .../newpipe/error/AppLifecycleObserver.kt | 42 ------------------- .../org/schabi/newpipe/error/ErrorUtil.kt | 9 ++-- 4 files changed, 29 insertions(+), 57 deletions(-) delete mode 100644 app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt diff --git a/app/src/main/java/org/schabi/newpipe/App.java b/app/src/main/java/org/schabi/newpipe/App.java index 39f21504b6e..9bc25d55d7f 100644 --- a/app/src/main/java/org/schabi/newpipe/App.java +++ b/app/src/main/java/org/schabi/newpipe/App.java @@ -8,14 +8,12 @@ import androidx.annotation.NonNull; import androidx.core.app.NotificationChannelCompat; import androidx.core.app.NotificationManagerCompat; -import androidx.lifecycle.ProcessLifecycleOwner; import androidx.preference.PreferenceManager; import com.jakewharton.processphoenix.ProcessPhoenix; import org.acra.ACRA; import org.acra.config.CoreConfigurationBuilder; -import org.schabi.newpipe.error.AppLifecycleObserver; import org.schabi.newpipe.error.ReCaptchaActivity; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.downloader.Downloader; @@ -81,9 +79,6 @@ protected void attachBaseContext(final Context base) { @Override public void onCreate() { super.onCreate(); - // Initialize the AppLifecycleObserver - AppLifecycleObserver.INSTANCE.initialize(this); - ProcessLifecycleOwner.get().getLifecycle().addObserver(AppLifecycleObserver.INSTANCE); app = this; diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index 17569412572..3f6cfdc586a 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -123,7 +123,10 @@ public class MainActivity extends AppCompatActivity { private static final int ITEM_ID_ABOUT = 1; private static final int ORDER = 0; + public static final String KEY_IS_IN_BACKGROUND = "is_in_background"; + private SharedPreferences sharedPreferences; + private SharedPreferences.Editor sharedPrefEditor; /*////////////////////////////////////////////////////////////////////////// // Activity's LifeCycle //////////////////////////////////////////////////////////////////////////*/ @@ -140,6 +143,8 @@ protected void onCreate(final Bundle savedInstanceState) { assureCorrectAppLanguage(this); super.onCreate(savedInstanceState); + sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + sharedPrefEditor = sharedPreferences.edit(); mainBinding = ActivityMainBinding.inflate(getLayoutInflater()); drawerLayoutBinding = mainBinding.drawerLayout; @@ -181,16 +186,29 @@ protected void onPostCreate(final Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); final App app = App.getApp(); - final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(app); - if (prefs.getBoolean(app.getString(R.string.update_app_key), false) - && prefs.getBoolean(app.getString(R.string.update_check_consent_key), false)) { + if (sharedPreferences.getBoolean(app.getString(R.string.update_app_key), false) + && sharedPreferences + .getBoolean(app.getString(R.string.update_check_consent_key), false)) { // Start the worker which is checking all conditions // and eventually searching for a new version. NewVersionWorker.enqueueNewVersionCheckingWork(app, false); } } + @Override + protected void onStart() { + super.onStart(); + sharedPrefEditor.putBoolean(KEY_IS_IN_BACKGROUND, false).apply(); + Log.d(TAG, "App moved to foreground"); + } + + @Override + protected void onStop() { + super.onStop(); + sharedPrefEditor.putBoolean(KEY_IS_IN_BACKGROUND, true).apply(); + Log.d(TAG, "App moved to background"); + } private void setupDrawer() throws ExtractionException { addDrawerMenuForCurrentService(); @@ -483,13 +501,11 @@ protected void onResume() { ErrorUtil.showUiErrorSnackbar(this, "Setting up service toggle", e); } - final SharedPreferences sharedPreferences = - PreferenceManager.getDefaultSharedPreferences(this); if (sharedPreferences.getBoolean(Constants.KEY_THEME_CHANGE, false)) { if (DEBUG) { Log.d(TAG, "Theme has changed, recreating activity..."); } - sharedPreferences.edit().putBoolean(Constants.KEY_THEME_CHANGE, false).apply(); + sharedPrefEditor.putBoolean(Constants.KEY_THEME_CHANGE, false).apply(); ActivityCompat.recreate(this); } @@ -497,7 +513,7 @@ protected void onResume() { if (DEBUG) { Log.d(TAG, "main page has changed, recreating main fragment..."); } - sharedPreferences.edit().putBoolean(Constants.KEY_MAIN_PAGE_CHANGE, false).apply(); + sharedPrefEditor.putBoolean(Constants.KEY_MAIN_PAGE_CHANGE, false).apply(); NavigationHelper.openMainActivity(this); } diff --git a/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt b/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt deleted file mode 100644 index 7a3d9b857e2..00000000000 --- a/app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt +++ /dev/null @@ -1,42 +0,0 @@ -package org.schabi.newpipe.error - -import android.content.Context -import android.content.SharedPreferences -import android.util.Log -import androidx.lifecycle.DefaultLifecycleObserver -import androidx.lifecycle.LifecycleOwner -import androidx.preference.PreferenceManager - -object AppLifecycleObserver : DefaultLifecycleObserver { - private const val KEY_IS_IN_BACKGROUND = "is_in_background" - private var TAG = javaClass.simpleName - private lateinit var sharedPreferences: SharedPreferences - private lateinit var editor: SharedPreferences.Editor - - // Only call this once on startup - fun initialize(context: Context) { - if (!this::sharedPreferences.isInitialized) { - sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) - editor = sharedPreferences.edit() - } - } - - override fun onStart(owner: LifecycleOwner) { - editor.putBoolean(KEY_IS_IN_BACKGROUND, false).commit() - Log.d(TAG, "App moved to foreground") - } - - override fun onPause(owner: LifecycleOwner) { - editor.putBoolean(KEY_IS_IN_BACKGROUND, true).commit() - Log.d(TAG, "App moved to background") - } - - - /** - * Returns if the app is currently in the background - * or in case of a crash the state when the crash happened - */ - fun isInBackground(): Boolean { - return sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true) - } -} diff --git a/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt b/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt index f89cac347d2..93dd8e522f3 100644 --- a/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt +++ b/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt @@ -11,9 +11,10 @@ import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import androidx.core.app.PendingIntentCompat import androidx.fragment.app.Fragment +import androidx.preference.PreferenceManager import com.google.android.material.snackbar.Snackbar +import org.schabi.newpipe.MainActivity import org.schabi.newpipe.R -import org.schabi.newpipe.error.AppLifecycleObserver.isInBackground /** * This class contains all of the methods that should be used to let the user know that an error has @@ -36,14 +37,16 @@ class ErrorUtil { * activity (since the workflow would be interrupted anyway in that case). So never use this * for background services. * - * If the crashed while the app was in the background open a notification instead + * If the crashed occurred while the app was in the background open a notification instead * * @param context the context to use to start the new activity * @param errorInfo the error info to be reported */ @JvmStatic fun openActivity(context: Context, errorInfo: ErrorInfo) { - if (isInBackground()) { + if (PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(MainActivity.KEY_IS_IN_BACKGROUND, true) + ) { createNotification(context, errorInfo) } else { context.startActivity(getErrorActivityIntent(context, errorInfo)) From 5dc48af32d542dab0be722742ba6b9643283763a Mon Sep 17 00:00:00 2001 From: Thompson3142 Date: Tue, 17 Dec 2024 19:49:43 +0100 Subject: [PATCH 7/7] Remove no longer needed dependency --- app/build.gradle | 1 - 1 file changed, 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index c082ef06e2d..662437f642a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -227,7 +227,6 @@ dependencies { implementation 'androidx.fragment:fragment-ktx:1.6.2' implementation "androidx.lifecycle:lifecycle-livedata-ktx:${androidxLifecycleVersion}" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${androidxLifecycleVersion}" - implementation "androidx.lifecycle:lifecycle-process:${androidxLifecycleVersion}" implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.1.0' implementation 'androidx.media:media:1.7.0' implementation 'androidx.preference:preference:1.2.1'