A few minor improvements
This commit is contained in:
parent
bdbdc29494
commit
9ebae13a43
|
@ -81,6 +81,9 @@ public class App extends Application {
|
|||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
// Initialize the AppLifecycleObserver
|
||||
AppLifecycleObserver.INSTANCE.initialize(this);
|
||||
ProcessLifecycleOwner.get().getLifecycle().addObserver(AppLifecycleObserver.INSTANCE);
|
||||
|
||||
app = this;
|
||||
|
||||
|
@ -96,9 +99,6 @@ 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);
|
||||
|
||||
|
|
|
@ -11,27 +11,27 @@ object AppLifecycleObserver : DefaultLifecycleObserver {
|
|||
private const val KEY_IS_IN_BACKGROUND = "is_in_background"
|
||||
private var TAG = javaClass.simpleName
|
||||
private lateinit var sharedPreferences: SharedPreferences
|
||||
private lateinit var editor: SharedPreferences.Editor
|
||||
|
||||
// Only call this once on startup
|
||||
fun initialize(context: Context) {
|
||||
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
if (!this::sharedPreferences.isInitialized) {
|
||||
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
editor = sharedPreferences.edit()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStart(owner: LifecycleOwner) {
|
||||
sharedPreferences.edit().putBoolean(KEY_IS_IN_BACKGROUND, false).apply()
|
||||
Log.d(TAG, "App moved to foreground")
|
||||
editor.putBoolean(KEY_IS_IN_BACKGROUND, false).commit()
|
||||
Log.d(TAG, "App moved to foreground: ")
|
||||
}
|
||||
|
||||
override fun onStop(owner: LifecycleOwner) {
|
||||
sharedPreferences.edit().putBoolean(KEY_IS_IN_BACKGROUND, true).apply()
|
||||
Log.d(TAG, "App moved to background")
|
||||
override fun onPause(owner: LifecycleOwner) {
|
||||
editor.putBoolean(KEY_IS_IN_BACKGROUND, true).commit()
|
||||
Log.d(TAG, "App moved to background: ")
|
||||
}
|
||||
|
||||
fun isInBackground(): Boolean {
|
||||
Log.d(
|
||||
TAG,
|
||||
"Is in background? -" +
|
||||
sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true)
|
||||
)
|
||||
return sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ import androidx.core.app.PendingIntentCompat
|
|||
import androidx.fragment.app.Fragment
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
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
|
||||
|
@ -35,15 +36,15 @@ class ErrorUtil {
|
|||
* activity (since the workflow would be interrupted anyway in that case). So never use this
|
||||
* for background services.
|
||||
*
|
||||
* If this method is called while the app has been in the background for more than
|
||||
* 10 seconds it will not start an error activity and instead create a notification
|
||||
* If this method is called was called while the app was in the background previously open
|
||||
* a notification instead
|
||||
*
|
||||
* @param context the context to use to start the new activity
|
||||
* @param errorInfo the error info to be reported
|
||||
*/
|
||||
@JvmStatic
|
||||
fun openActivity(context: Context, errorInfo: ErrorInfo) {
|
||||
if (AppLifecycleObserver.isInBackground()) {
|
||||
if (isInBackground()) {
|
||||
createNotification(context, errorInfo)
|
||||
} else {
|
||||
context.startActivity(getErrorActivityIntent(context, errorInfo))
|
||||
|
|
Loading…
Reference in New Issue