1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-02-02 20:29:15 +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.annotation.NonNull;
import androidx.core.app.NotificationChannelCompat; import androidx.core.app.NotificationChannelCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
import androidx.lifecycle.ProcessLifecycleOwner;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import com.jakewharton.processphoenix.ProcessPhoenix; import com.jakewharton.processphoenix.ProcessPhoenix;
import org.acra.ACRA; import org.acra.ACRA;
import org.acra.config.CoreConfigurationBuilder; import org.acra.config.CoreConfigurationBuilder;
import org.schabi.newpipe.error.AppLifecycleObserver;
import org.schabi.newpipe.error.ReCaptchaActivity; import org.schabi.newpipe.error.ReCaptchaActivity;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.downloader.Downloader; 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); .getInt(getString(R.string.last_used_preferences_version), -1);
isFirstRun = lastUsedPrefVersion == -1; isFirstRun = lastUsedPrefVersion == -1;
AppLifecycleObserver.INSTANCE.initialize(this);
ProcessLifecycleOwner.get().getLifecycle().addObserver(AppLifecycleObserver.INSTANCE);
// Initialize settings first because other initializations can use its values // Initialize settings first because other initializations can use its values
NewPipeSettings.initSettings(this); NewPipeSettings.initSettings(this);

View File

@ -1,32 +1,37 @@
package org.schabi.newpipe.error package org.schabi.newpipe.error
import android.content.Context
import android.content.SharedPreferences
import android.util.Log import android.util.Log
import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
import java.util.concurrent.atomic.AtomicLong import androidx.preference.PreferenceManager
object AppLifecycleObserver : DefaultLifecycleObserver { object AppLifecycleObserver : DefaultLifecycleObserver {
private var isInBackground = false private const val KEY_IS_IN_BACKGROUND = "is_in_background"
private val lastBackgroundTimestamp = AtomicLong(0)
private var TAG = javaClass.simpleName private var TAG = javaClass.simpleName
private lateinit var sharedPreferences: SharedPreferences
fun initialize(context: Context) {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
}
override fun onStart(owner: LifecycleOwner) { override fun onStart(owner: LifecycleOwner) {
isInBackground = false sharedPreferences.edit().putBoolean(KEY_IS_IN_BACKGROUND, false).apply()
Log.d(TAG, "App moved to foreground") Log.d(TAG, "App moved to foreground")
} }
override fun onStop(owner: LifecycleOwner) { override fun onStop(owner: LifecycleOwner) {
isInBackground = true sharedPreferences.edit().putBoolean(KEY_IS_IN_BACKGROUND, true).apply()
lastBackgroundTimestamp.set(System.currentTimeMillis())
Log.d(TAG, "App moved to background") Log.d(TAG, "App moved to background")
} }
fun isAppInBackground(): Boolean = isInBackground fun isInBackground(): Boolean {
Log.d(
/** TAG,
* @return the elapsed time since the app moved to the background or 0 if it is in foreground "Is in background? -" +
*/ sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true)
fun getTimeSinceLastBackground(): Long { )
return if (isInBackground) System.currentTimeMillis() - lastBackgroundTimestamp.get() else 0 return sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true)
} }
} }

View File

@ -43,7 +43,7 @@ class ErrorUtil {
*/ */
@JvmStatic @JvmStatic
fun openActivity(context: Context, errorInfo: ErrorInfo) { fun openActivity(context: Context, errorInfo: ErrorInfo) {
if (AppLifecycleObserver.getTimeSinceLastBackground() > 10000) { if (AppLifecycleObserver.isInBackground()) {
createNotification(context, errorInfo) createNotification(context, errorInfo)
} else { } else {
context.startActivity(getErrorActivityIntent(context, errorInfo)) 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.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapterMenuWorkaround; import androidx.fragment.app.FragmentStatePagerAdapterMenuWorkaround;
import androidx.lifecycle.ProcessLifecycleOwner;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import androidx.viewpager.widget.ViewPager; 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.BaseFragment;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.databinding.FragmentMainBinding; import org.schabi.newpipe.databinding.FragmentMainBinding;
import org.schabi.newpipe.error.AppLifecycleObserver;
import org.schabi.newpipe.error.ErrorUtil; import org.schabi.newpipe.error.ErrorUtil;
import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.local.playlist.LocalPlaylistFragment; import org.schabi.newpipe.local.playlist.LocalPlaylistFragment;
@ -73,7 +71,6 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
@Override @Override
public void onCreate(final Bundle savedInstanceState) { public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
ProcessLifecycleOwner.get().getLifecycle().addObserver(AppLifecycleObserver.INSTANCE);
setHasOptionsMenu(true); setHasOptionsMenu(true);
tabsManager = TabsManager.getManager(activity); tabsManager = TabsManager.getManager(activity);
tabsManager.setSavedTabsListener(() -> { tabsManager.setSavedTabsListener(() -> {