Skip to content

Commit 90e2f23

Browse files
Thompson3142Profpatsch
authored andcommitted
Initial commit for better handling of background crashes
Fix crashing behaviour with entry in SharedPreferences A few minor improvements Added docs for isInBackground Some more minor changes Overwrite methods in MainActivity instead of creating a new class
1 parent 42a52b7 commit 90e2f23

3 files changed

Lines changed: 35 additions & 8 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ dependencies {
233233
implementation 'androidx.fragment:fragment-ktx:1.6.2'
234234
implementation "androidx.lifecycle:lifecycle-livedata-ktx:${androidxLifecycleVersion}"
235235
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${androidxLifecycleVersion}"
236+
implementation "androidx.lifecycle:lifecycle-process:${androidxLifecycleVersion}"
236237
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.1.0'
237238
implementation 'androidx.media:media:1.7.0'
238239
implementation 'androidx.preference:preference:1.2.1'

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/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)