Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix background crash focus #11789

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/org/schabi/newpipe/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,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;

Expand Down
42 changes: 42 additions & 0 deletions app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you override these functions directly in MainActivity?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can, i just missed that AppCompatActivity has these methods to overwrite. Unfortunately it does not seem like it fixes the occasionally wrong behavior i mentioned, probably something related to ACRA but I am unsure how to fix it.



/**
* 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)
}
}
9 changes: 8 additions & 1 deletion app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,12 +36,18 @@ 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
*
* @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 (isInBackground()) {
createNotification(context, errorInfo)
} else {
context.startActivity(getErrorActivityIntent(context, errorInfo))
}
}

/**
Expand Down
Loading