Skip to content

Commit 09e2f8f

Browse files
committed
Overwrite methods in MainActivity instead of creating a new class
1 parent 2cf584b commit 09e2f8f

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;
@@ -81,9 +79,6 @@ protected void attachBaseContext(final Context base) {
8179
@Override
8280
public void onCreate() {
8381
super.onCreate();
84-
// Initialize the AppLifecycleObserver
85-
AppLifecycleObserver.INSTANCE.initialize(this);
86-
ProcessLifecycleOwner.get().getLifecycle().addObserver(AppLifecycleObserver.INSTANCE);
8782

8883
app = this;
8984

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ public class MainActivity extends AppCompatActivity {
123123
private static final int ITEM_ID_ABOUT = 1;
124124

125125
private static final int ORDER = 0;
126+
public static final String KEY_IS_IN_BACKGROUND = "is_in_background";
126127

128+
private SharedPreferences sharedPreferences;
129+
private SharedPreferences.Editor sharedPrefEditor;
127130
/*//////////////////////////////////////////////////////////////////////////
128131
// Activity's LifeCycle
129132
//////////////////////////////////////////////////////////////////////////*/
@@ -140,6 +143,8 @@ protected void onCreate(final Bundle savedInstanceState) {
140143

141144
assureCorrectAppLanguage(this);
142145
super.onCreate(savedInstanceState);
146+
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
147+
sharedPrefEditor = sharedPreferences.edit();
143148

144149
mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
145150
drawerLayoutBinding = mainBinding.drawerLayout;
@@ -181,16 +186,29 @@ protected void onPostCreate(final Bundle savedInstanceState) {
181186
super.onPostCreate(savedInstanceState);
182187

183188
final App app = App.getApp();
184-
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(app);
185189

186-
if (prefs.getBoolean(app.getString(R.string.update_app_key), false)
187-
&& prefs.getBoolean(app.getString(R.string.update_check_consent_key), false)) {
190+
if (sharedPreferences.getBoolean(app.getString(R.string.update_app_key), false)
191+
&& sharedPreferences
192+
.getBoolean(app.getString(R.string.update_check_consent_key), false)) {
188193
// Start the worker which is checking all conditions
189194
// and eventually searching for a new version.
190195
NewVersionWorker.enqueueNewVersionCheckingWork(app, false);
191196
}
192197
}
193198

199+
@Override
200+
protected void onStart() {
201+
super.onStart();
202+
sharedPrefEditor.putBoolean(KEY_IS_IN_BACKGROUND, false).apply();
203+
Log.d(TAG, "App moved to foreground");
204+
}
205+
206+
@Override
207+
protected void onStop() {
208+
super.onStop();
209+
sharedPrefEditor.putBoolean(KEY_IS_IN_BACKGROUND, true).apply();
210+
Log.d(TAG, "App moved to background");
211+
}
194212
private void setupDrawer() throws ExtractionException {
195213
addDrawerMenuForCurrentService();
196214

@@ -483,21 +501,19 @@ protected void onResume() {
483501
ErrorUtil.showUiErrorSnackbar(this, "Setting up service toggle", e);
484502
}
485503

486-
final SharedPreferences sharedPreferences =
487-
PreferenceManager.getDefaultSharedPreferences(this);
488504
if (sharedPreferences.getBoolean(Constants.KEY_THEME_CHANGE, false)) {
489505
if (DEBUG) {
490506
Log.d(TAG, "Theme has changed, recreating activity...");
491507
}
492-
sharedPreferences.edit().putBoolean(Constants.KEY_THEME_CHANGE, false).apply();
508+
sharedPrefEditor.putBoolean(Constants.KEY_THEME_CHANGE, false).apply();
493509
ActivityCompat.recreate(this);
494510
}
495511

496512
if (sharedPreferences.getBoolean(Constants.KEY_MAIN_PAGE_CHANGE, false)) {
497513
if (DEBUG) {
498514
Log.d(TAG, "main page has changed, recreating main fragment...");
499515
}
500-
sharedPreferences.edit().putBoolean(Constants.KEY_MAIN_PAGE_CHANGE, false).apply();
516+
sharedPrefEditor.putBoolean(Constants.KEY_MAIN_PAGE_CHANGE, false).apply();
501517
NavigationHelper.openMainActivity(this);
502518
}
503519

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)