From 90e2f234e72b8ce4b95aba64173db1a848c395fb Mon Sep 17 00:00:00 2001 From: Thompson3142 Date: Tue, 3 Dec 2024 00:51:15 +0100 Subject: [PATCH 1/2] Initial commit for better handling of background crashes Fix crashing behaviour with entry in SharedPreferences A few minor improvements Added docs for isInBackground Some more minor changes Overwrite methods in MainActivity instead of creating a new class --- app/build.gradle | 1 + .../java/org/schabi/newpipe/MainActivity.java | 30 ++++++++++++++----- .../org/schabi/newpipe/error/ErrorUtil.kt | 12 +++++++- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 5a5a2be1b..6fc06aadc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -233,6 +233,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/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index f74e8b7a7..b709c1107 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -126,7 +126,10 @@ public class MainActivity extends AppCompatActivity { private static final int ITEM_ID_ABOUT = 2; 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 //////////////////////////////////////////////////////////////////////////*/ @@ -156,6 +159,8 @@ public class MainActivity extends AppCompatActivity { assureCorrectAppLanguage(this); super.onCreate(savedInstanceState); + sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + sharedPrefEditor = sharedPreferences.edit(); mainBinding = ActivityMainBinding.inflate(getLayoutInflater()); drawerLayoutBinding = mainBinding.drawerLayout; @@ -199,16 +204,29 @@ public class MainActivity extends AppCompatActivity { 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(); @@ -508,13 +526,11 @@ public class MainActivity extends AppCompatActivity { 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); } @@ -522,7 +538,7 @@ public class MainActivity extends AppCompatActivity { 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/ErrorUtil.kt b/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt index dcbc11413..93dd8e522 100644 --- a/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt +++ b/app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt @@ -11,7 +11,9 @@ 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 /** @@ -35,12 +37,20 @@ class ErrorUtil { * activity (since the workflow would be interrupted anyway in that case). So never use this * for background services. * + * 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) { - context.startActivity(getErrorActivityIntent(context, errorInfo)) + if (PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(MainActivity.KEY_IS_IN_BACKGROUND, true) + ) { + createNotification(context, errorInfo) + } else { + context.startActivity(getErrorActivityIntent(context, errorInfo)) + } } /** From 76202e6b4b20e1d4bd17359675015cfed08ca7f5 Mon Sep 17 00:00:00 2001 From: Thompson3142 Date: Tue, 17 Dec 2024 19:49:43 +0100 Subject: [PATCH 2/2] 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 6fc06aadc..5a5a2be1b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -233,7 +233,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'