Skip to content

Commit 9ebae13

Browse files
committed
A few minor improvements
1 parent bdbdc29 commit 9ebae13

3 files changed

Lines changed: 18 additions & 17 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ protected void attachBaseContext(final Context base) {
8181
@Override
8282
public void onCreate() {
8383
super.onCreate();
84+
// Initialize the AppLifecycleObserver
85+
AppLifecycleObserver.INSTANCE.initialize(this);
86+
ProcessLifecycleOwner.get().getLifecycle().addObserver(AppLifecycleObserver.INSTANCE);
8487

8588
app = this;
8689

@@ -96,9 +99,6 @@ public void onCreate() {
9699
.getInt(getString(R.string.last_used_preferences_version), -1);
97100
isFirstRun = lastUsedPrefVersion == -1;
98101

99-
AppLifecycleObserver.INSTANCE.initialize(this);
100-
ProcessLifecycleOwner.get().getLifecycle().addObserver(AppLifecycleObserver.INSTANCE);
101-
102102
// Initialize settings first because other initializations can use its values
103103
NewPipeSettings.initSettings(this);
104104

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@ object AppLifecycleObserver : DefaultLifecycleObserver {
1111
private const val KEY_IS_IN_BACKGROUND = "is_in_background"
1212
private var TAG = javaClass.simpleName
1313
private lateinit var sharedPreferences: SharedPreferences
14+
private lateinit var editor: SharedPreferences.Editor
1415

16+
// Only call this once on startup
1517
fun initialize(context: Context) {
16-
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
18+
if (!this::sharedPreferences.isInitialized) {
19+
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
20+
editor = sharedPreferences.edit()
21+
}
1722
}
1823

1924
override fun onStart(owner: LifecycleOwner) {
20-
sharedPreferences.edit().putBoolean(KEY_IS_IN_BACKGROUND, false).apply()
21-
Log.d(TAG, "App moved to foreground")
25+
editor.putBoolean(KEY_IS_IN_BACKGROUND, false).commit()
26+
Log.d(TAG, "App moved to foreground: ")
2227
}
2328

24-
override fun onStop(owner: LifecycleOwner) {
25-
sharedPreferences.edit().putBoolean(KEY_IS_IN_BACKGROUND, true).apply()
26-
Log.d(TAG, "App moved to background")
29+
override fun onPause(owner: LifecycleOwner) {
30+
editor.putBoolean(KEY_IS_IN_BACKGROUND, true).commit()
31+
Log.d(TAG, "App moved to background: ")
2732
}
2833

2934
fun isInBackground(): Boolean {
30-
Log.d(
31-
TAG,
32-
"Is in background? -" +
33-
sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true)
34-
)
3535
return sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true)
3636
}
3737
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import androidx.core.app.PendingIntentCompat
1313
import androidx.fragment.app.Fragment
1414
import com.google.android.material.snackbar.Snackbar
1515
import org.schabi.newpipe.R
16+
import org.schabi.newpipe.error.AppLifecycleObserver.isInBackground
1617

1718
/**
1819
* This class contains all of the methods that should be used to let the user know that an error has
@@ -35,15 +36,15 @@ class ErrorUtil {
3536
* activity (since the workflow would be interrupted anyway in that case). So never use this
3637
* for background services.
3738
*
38-
* If this method is called while the app has been in the background for more than
39-
* 10 seconds it will not start an error activity and instead create a notification
39+
* If this method is called was called while the app was in the background previously open
40+
* a notification instead
4041
*
4142
* @param context the context to use to start the new activity
4243
* @param errorInfo the error info to be reported
4344
*/
4445
@JvmStatic
4546
fun openActivity(context: Context, errorInfo: ErrorInfo) {
46-
if (AppLifecycleObserver.isInBackground()) {
47+
if (isInBackground()) {
4748
createNotification(context, errorInfo)
4849
} else {
4950
context.startActivity(getErrorActivityIntent(context, errorInfo))

0 commit comments

Comments
 (0)