Skip to content

Commit 4652aed

Browse files
Thompson3142Profpatsch
authored andcommitted
Overwrite methods in MainActivity instead of creating a new class
1 parent 0e636b3 commit 4652aed

4 files changed

Lines changed: 29 additions & 57 deletions

File tree

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

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

1413
import com.jakewharton.processphoenix.ProcessPhoenix;
1514

1615
import org.acra.ACRA;
1716
import org.acra.config.CoreConfigurationBuilder;
18-
import org.schabi.newpipe.error.AppLifecycleObserver;
1917
import org.schabi.newpipe.error.ReCaptchaActivity;
2018
import org.schabi.newpipe.extractor.NewPipe;
2119
import org.schabi.newpipe.extractor.downloader.Downloader;
@@ -83,9 +81,6 @@ protected void attachBaseContext(final Context base) {
8381
@Override
8482
public void onCreate() {
8583
super.onCreate();
86-
// Initialize the AppLifecycleObserver
87-
AppLifecycleObserver.INSTANCE.initialize(this);
88-
ProcessLifecycleOwner.get().getLifecycle().addObserver(AppLifecycleObserver.INSTANCE);
8984

9085
app = this;
9186

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ public class MainActivity extends AppCompatActivity {
126126
private static final int ITEM_ID_ABOUT = 2;
127127

128128
private static final int ORDER = 0;
129+
public static final String KEY_IS_IN_BACKGROUND = "is_in_background";
129130

131+
private SharedPreferences sharedPreferences;
132+
private SharedPreferences.Editor sharedPrefEditor;
130133
/*//////////////////////////////////////////////////////////////////////////
131134
// Activity's LifeCycle
132135
//////////////////////////////////////////////////////////////////////////*/
@@ -156,6 +159,8 @@ protected void onCreate(final Bundle savedInstanceState) {
156159

157160
assureCorrectAppLanguage(this);
158161
super.onCreate(savedInstanceState);
162+
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
163+
sharedPrefEditor = sharedPreferences.edit();
159164

160165
mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
161166
drawerLayoutBinding = mainBinding.drawerLayout;
@@ -199,16 +204,29 @@ protected void onPostCreate(final Bundle savedInstanceState) {
199204
super.onPostCreate(savedInstanceState);
200205

201206
final App app = App.getApp();
202-
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(app);
203207

204-
if (prefs.getBoolean(app.getString(R.string.update_app_key), false)
205-
&& prefs.getBoolean(app.getString(R.string.update_check_consent_key), false)) {
208+
if (sharedPreferences.getBoolean(app.getString(R.string.update_app_key), false)
209+
&& sharedPreferences
210+
.getBoolean(app.getString(R.string.update_check_consent_key), false)) {
206211
// Start the worker which is checking all conditions
207212
// and eventually searching for a new version.
208213
NewVersionWorker.enqueueNewVersionCheckingWork(app, false);
209214
}
210215
}
211216

217+
@Override
218+
protected void onStart() {
219+
super.onStart();
220+
sharedPrefEditor.putBoolean(KEY_IS_IN_BACKGROUND, false).apply();
221+
Log.d(TAG, "App moved to foreground");
222+
}
223+
224+
@Override
225+
protected void onStop() {
226+
super.onStop();
227+
sharedPrefEditor.putBoolean(KEY_IS_IN_BACKGROUND, true).apply();
228+
Log.d(TAG, "App moved to background");
229+
}
212230
private void setupDrawer() throws ExtractionException {
213231
addDrawerMenuForCurrentService();
214232

@@ -508,21 +526,19 @@ protected void onResume() {
508526
ErrorUtil.showUiErrorSnackbar(this, "Setting up service toggle", e);
509527
}
510528

511-
final SharedPreferences sharedPreferences =
512-
PreferenceManager.getDefaultSharedPreferences(this);
513529
if (sharedPreferences.getBoolean(Constants.KEY_THEME_CHANGE, false)) {
514530
if (DEBUG) {
515531
Log.d(TAG, "Theme has changed, recreating activity...");
516532
}
517-
sharedPreferences.edit().putBoolean(Constants.KEY_THEME_CHANGE, false).apply();
533+
sharedPrefEditor.putBoolean(Constants.KEY_THEME_CHANGE, false).apply();
518534
ActivityCompat.recreate(this);
519535
}
520536

521537
if (sharedPreferences.getBoolean(Constants.KEY_MAIN_PAGE_CHANGE, false)) {
522538
if (DEBUG) {
523539
Log.d(TAG, "main page has changed, recreating main fragment...");
524540
}
525-
sharedPreferences.edit().putBoolean(Constants.KEY_MAIN_PAGE_CHANGE, false).apply();
541+
sharedPrefEditor.putBoolean(Constants.KEY_MAIN_PAGE_CHANGE, false).apply();
526542
NavigationHelper.openMainActivity(this);
527543
}
528544

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

Lines changed: 0 additions & 42 deletions
This file was deleted.

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import androidx.core.app.NotificationCompat
1111
import androidx.core.app.NotificationManagerCompat
1212
import androidx.core.app.PendingIntentCompat
1313
import androidx.fragment.app.Fragment
14+
import androidx.preference.PreferenceManager
1415
import com.google.android.material.snackbar.Snackbar
16+
import org.schabi.newpipe.MainActivity
1517
import org.schabi.newpipe.R
16-
import org.schabi.newpipe.error.AppLifecycleObserver.isInBackground
1718

1819
/**
1920
* This class contains all of the methods that should be used to let the user know that an error has
@@ -36,14 +37,16 @@ class ErrorUtil {
3637
* activity (since the workflow would be interrupted anyway in that case). So never use this
3738
* for background services.
3839
*
39-
* If the crashed while the app was in the background open a notification instead
40+
* If the crashed occurred while the app was in the background open 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 (isInBackground()) {
47+
if (PreferenceManager.getDefaultSharedPreferences(context)
48+
.getBoolean(MainActivity.KEY_IS_IN_BACKGROUND, true)
49+
) {
4750
createNotification(context, errorInfo)
4851
} else {
4952
context.startActivity(getErrorActivityIntent(context, errorInfo))

0 commit comments

Comments
 (0)