Skip to content

Commit bdbdc29

Browse files
committed
Fix crashing behaviour with entry in SharedPreferences
1 parent 402f0b1 commit bdbdc29

4 files changed

Lines changed: 24 additions & 17 deletions

File tree

app/src/main/java/org/schabi/newpipe/App.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
import androidx.annotation.NonNull;
99
import androidx.core.app.NotificationChannelCompat;
1010
import androidx.core.app.NotificationManagerCompat;
11+
import androidx.lifecycle.ProcessLifecycleOwner;
1112
import androidx.preference.PreferenceManager;
1213

1314
import com.jakewharton.processphoenix.ProcessPhoenix;
1415

1516
import org.acra.ACRA;
1617
import org.acra.config.CoreConfigurationBuilder;
18+
import org.schabi.newpipe.error.AppLifecycleObserver;
1719
import org.schabi.newpipe.error.ReCaptchaActivity;
1820
import org.schabi.newpipe.extractor.NewPipe;
1921
import org.schabi.newpipe.extractor.downloader.Downloader;
@@ -94,6 +96,9 @@ public void onCreate() {
9496
.getInt(getString(R.string.last_used_preferences_version), -1);
9597
isFirstRun = lastUsedPrefVersion == -1;
9698

99+
AppLifecycleObserver.INSTANCE.initialize(this);
100+
ProcessLifecycleOwner.get().getLifecycle().addObserver(AppLifecycleObserver.INSTANCE);
101+
97102
// Initialize settings first because other initializations can use its values
98103
NewPipeSettings.initSettings(this);
99104

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
package org.schabi.newpipe.error
22

3+
import android.content.Context
4+
import android.content.SharedPreferences
35
import android.util.Log
46
import androidx.lifecycle.DefaultLifecycleObserver
57
import androidx.lifecycle.LifecycleOwner
6-
import java.util.concurrent.atomic.AtomicLong
8+
import androidx.preference.PreferenceManager
79

810
object AppLifecycleObserver : DefaultLifecycleObserver {
9-
private var isInBackground = false
10-
private val lastBackgroundTimestamp = AtomicLong(0)
11+
private const val KEY_IS_IN_BACKGROUND = "is_in_background"
1112
private var TAG = javaClass.simpleName
13+
private lateinit var sharedPreferences: SharedPreferences
14+
15+
fun initialize(context: Context) {
16+
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
17+
}
1218

1319
override fun onStart(owner: LifecycleOwner) {
14-
isInBackground = false
20+
sharedPreferences.edit().putBoolean(KEY_IS_IN_BACKGROUND, false).apply()
1521
Log.d(TAG, "App moved to foreground")
1622
}
1723

1824
override fun onStop(owner: LifecycleOwner) {
19-
isInBackground = true
20-
lastBackgroundTimestamp.set(System.currentTimeMillis())
25+
sharedPreferences.edit().putBoolean(KEY_IS_IN_BACKGROUND, true).apply()
2126
Log.d(TAG, "App moved to background")
2227
}
2328

24-
fun isAppInBackground(): Boolean = isInBackground
25-
26-
/**
27-
* @return the elapsed time since the app moved to the background or 0 if it is in foreground
28-
*/
29-
fun getTimeSinceLastBackground(): Long {
30-
return if (isInBackground) System.currentTimeMillis() - lastBackgroundTimestamp.get() else 0
29+
fun isInBackground(): Boolean {
30+
Log.d(
31+
TAG,
32+
"Is in background? -" +
33+
sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true)
34+
)
35+
return sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true)
3136
}
3237
}

app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ErrorUtil {
4343
*/
4444
@JvmStatic
4545
fun openActivity(context: Context, errorInfo: ErrorInfo) {
46-
if (AppLifecycleObserver.getTimeSinceLastBackground() > 10000) {
46+
if (AppLifecycleObserver.isInBackground()) {
4747
createNotification(context, errorInfo)
4848
} else {
4949
context.startActivity(getErrorActivityIntent(context, errorInfo))

app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import androidx.fragment.app.Fragment;
2929
import androidx.fragment.app.FragmentManager;
3030
import androidx.fragment.app.FragmentStatePagerAdapterMenuWorkaround;
31-
import androidx.lifecycle.ProcessLifecycleOwner;
3231
import androidx.preference.PreferenceManager;
3332
import androidx.viewpager.widget.ViewPager;
3433

@@ -37,7 +36,6 @@
3736
import org.schabi.newpipe.BaseFragment;
3837
import org.schabi.newpipe.R;
3938
import org.schabi.newpipe.databinding.FragmentMainBinding;
40-
import org.schabi.newpipe.error.AppLifecycleObserver;
4139
import org.schabi.newpipe.error.ErrorUtil;
4240
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
4341
import org.schabi.newpipe.local.playlist.LocalPlaylistFragment;
@@ -73,7 +71,6 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
7371
@Override
7472
public void onCreate(final Bundle savedInstanceState) {
7573
super.onCreate(savedInstanceState);
76-
ProcessLifecycleOwner.get().getLifecycle().addObserver(AppLifecycleObserver.INSTANCE);
7774
setHasOptionsMenu(true);
7875
tabsManager = TabsManager.getManager(activity);
7976
tabsManager.setSavedTabsListener(() -> {

0 commit comments

Comments
 (0)