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

A few minor improvements

This commit is contained in:
Thompson3142 2024-12-12 22:48:33 +01:00
parent bdbdc29494
commit 9ebae13a43
3 changed files with 18 additions and 17 deletions

View File

@ -81,6 +81,9 @@ public class App extends Application {
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
// Initialize the AppLifecycleObserver
AppLifecycleObserver.INSTANCE.initialize(this);
ProcessLifecycleOwner.get().getLifecycle().addObserver(AppLifecycleObserver.INSTANCE);
app = this; app = this;
@ -96,9 +99,6 @@ 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

@ -11,27 +11,27 @@ object AppLifecycleObserver : DefaultLifecycleObserver {
private const val KEY_IS_IN_BACKGROUND = "is_in_background" private const val KEY_IS_IN_BACKGROUND = "is_in_background"
private var TAG = javaClass.simpleName private var TAG = javaClass.simpleName
private lateinit var sharedPreferences: SharedPreferences private lateinit var sharedPreferences: SharedPreferences
private lateinit var editor: SharedPreferences.Editor
// Only call this once on startup
fun initialize(context: Context) { fun initialize(context: Context) {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) if (!this::sharedPreferences.isInitialized) {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
editor = sharedPreferences.edit()
}
} }
override fun onStart(owner: LifecycleOwner) { override fun onStart(owner: LifecycleOwner) {
sharedPreferences.edit().putBoolean(KEY_IS_IN_BACKGROUND, false).apply() editor.putBoolean(KEY_IS_IN_BACKGROUND, false).commit()
Log.d(TAG, "App moved to foreground") Log.d(TAG, "App moved to foreground: ")
} }
override fun onStop(owner: LifecycleOwner) { override fun onPause(owner: LifecycleOwner) {
sharedPreferences.edit().putBoolean(KEY_IS_IN_BACKGROUND, true).apply() editor.putBoolean(KEY_IS_IN_BACKGROUND, true).commit()
Log.d(TAG, "App moved to background") Log.d(TAG, "App moved to background: ")
} }
fun isInBackground(): Boolean { fun isInBackground(): Boolean {
Log.d(
TAG,
"Is in background? -" +
sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true)
)
return sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true) return sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true)
} }
} }

View File

@ -13,6 +13,7 @@ import androidx.core.app.PendingIntentCompat
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import org.schabi.newpipe.R 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 * 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 {
* activity (since the workflow would be interrupted anyway in that case). So never use this * activity (since the workflow would be interrupted anyway in that case). So never use this
* for background services. * for background services.
* *
* If this method is called while the app has been in the background for more than * If this method is called was called while the app was in the background previously open
* 10 seconds it will not start an error activity and instead create a notification * a notification instead
* *
* @param context the context to use to start the new activity * @param context the context to use to start the new activity
* @param errorInfo the error info to be reported * @param errorInfo the error info to be reported
*/ */
@JvmStatic @JvmStatic
fun openActivity(context: Context, errorInfo: ErrorInfo) { fun openActivity(context: Context, errorInfo: ErrorInfo) {
if (AppLifecycleObserver.isInBackground()) { if (isInBackground()) {
createNotification(context, errorInfo) createNotification(context, errorInfo)
} else { } else {
context.startActivity(getErrorActivityIntent(context, errorInfo)) context.startActivity(getErrorActivityIntent(context, errorInfo))