1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-01-23 15:36:57 +00:00

Fix crashing behaviour with entry in SharedPreferences

This commit is contained in:
Thompson3142 2024-12-03 23:18:33 +01:00
parent 402f0b15aa
commit bdbdc29494
4 changed files with 24 additions and 17 deletions

View File

@ -8,12 +8,14 @@ import android.util.Log;
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 class App extends Application {
.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);

View File

@ -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)
}
}

View File

@ -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))

View File

@ -28,7 +28,6 @@ import androidx.appcompat.app.ActionBar;
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 com.google.android.material.tabs.TabLayout;
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(() -> {