Skip to content

Commit a01764a

Browse files
Profpatschwhistlingwoods
authored andcommitted
Merge pull request TeamNewPipe#11789 from Thompson3142/fix_background_crash_focus
Fix background crash focus
1 parent 5411f8b commit a01764a

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

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

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

127127
private static final int ORDER = 0;
128+
public static final String KEY_IS_IN_BACKGROUND = "is_in_background";
128129

130+
private SharedPreferences sharedPreferences;
131+
private SharedPreferences.Editor sharedPrefEditor;
129132
/*//////////////////////////////////////////////////////////////////////////
130133
// Activity's LifeCycle
131134
//////////////////////////////////////////////////////////////////////////*/
@@ -142,6 +145,8 @@ protected void onCreate(final Bundle savedInstanceState) {
142145

143146
assureCorrectAppLanguage(this);
144147
super.onCreate(savedInstanceState);
148+
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
149+
sharedPrefEditor = sharedPreferences.edit();
145150

146151
mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
147152
drawerLayoutBinding = mainBinding.drawerLayout;
@@ -183,16 +188,29 @@ protected void onPostCreate(final Bundle savedInstanceState) {
183188
super.onPostCreate(savedInstanceState);
184189

185190
final App app = App.getApp();
186-
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(app);
187191

188-
if (prefs.getBoolean(app.getString(R.string.update_app_key), false)
189-
&& prefs.getBoolean(app.getString(R.string.update_check_consent_key), false)) {
192+
if (sharedPreferences.getBoolean(app.getString(R.string.update_app_key), false)
193+
&& sharedPreferences
194+
.getBoolean(app.getString(R.string.update_check_consent_key), false)) {
190195
// Start the worker which is checking all conditions
191196
// and eventually searching for a new version.
192197
NewVersionWorker.enqueueNewVersionCheckingWork(app, false);
193198
}
194199
}
195200

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

@@ -492,21 +510,19 @@ protected void onResume() {
492510
ErrorUtil.showUiErrorSnackbar(this, "Setting up service toggle", e);
493511
}
494512

495-
final SharedPreferences sharedPreferences =
496-
PreferenceManager.getDefaultSharedPreferences(this);
497513
if (sharedPreferences.getBoolean(Constants.KEY_THEME_CHANGE, false)) {
498514
if (DEBUG) {
499515
Log.d(TAG, "Theme has changed, recreating activity...");
500516
}
501-
sharedPreferences.edit().putBoolean(Constants.KEY_THEME_CHANGE, false).apply();
517+
sharedPrefEditor.putBoolean(Constants.KEY_THEME_CHANGE, false).apply();
502518
ActivityCompat.recreate(this);
503519
}
504520

505521
if (sharedPreferences.getBoolean(Constants.KEY_MAIN_PAGE_CHANGE, false)) {
506522
if (DEBUG) {
507523
Log.d(TAG, "main page has changed, recreating main fragment...");
508524
}
509-
sharedPreferences.edit().putBoolean(Constants.KEY_MAIN_PAGE_CHANGE, false).apply();
525+
sharedPrefEditor.putBoolean(Constants.KEY_MAIN_PAGE_CHANGE, false).apply();
510526
NavigationHelper.openMainActivity(this);
511527
}
512528

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ 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
1618

1719
/**
@@ -35,12 +37,20 @@ class ErrorUtil {
3537
* activity (since the workflow would be interrupted anyway in that case). So never use this
3638
* for background services.
3739
*
40+
* If the crashed occurred while the app was in the background open a notification instead
41+
*
3842
* @param context the context to use to start the new activity
3943
* @param errorInfo the error info to be reported
4044
*/
4145
@JvmStatic
4246
fun openActivity(context: Context, errorInfo: ErrorInfo) {
43-
context.startActivity(getErrorActivityIntent(context, errorInfo))
47+
if (PreferenceManager.getDefaultSharedPreferences(context)
48+
.getBoolean(MainActivity.KEY_IS_IN_BACKGROUND, true)
49+
) {
50+
createNotification(context, errorInfo)
51+
} else {
52+
context.startActivity(getErrorActivityIntent(context, errorInfo))
53+
}
4454
}
4555

4656
/**

0 commit comments

Comments
 (0)