mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2025-01-06 15:30:31 +00:00
Overwrite methods in MainActivity instead of creating a new class
This commit is contained in:
parent
2cf584b74b
commit
09e2f8f717
@ -8,14 +8,12 @@ import android.util.Log;
|
|||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.core.app.NotificationChannelCompat;
|
import androidx.core.app.NotificationChannelCompat;
|
||||||
import androidx.core.app.NotificationManagerCompat;
|
import androidx.core.app.NotificationManagerCompat;
|
||||||
import androidx.lifecycle.ProcessLifecycleOwner;
|
|
||||||
import androidx.preference.PreferenceManager;
|
import androidx.preference.PreferenceManager;
|
||||||
|
|
||||||
import com.jakewharton.processphoenix.ProcessPhoenix;
|
import com.jakewharton.processphoenix.ProcessPhoenix;
|
||||||
|
|
||||||
import org.acra.ACRA;
|
import org.acra.ACRA;
|
||||||
import org.acra.config.CoreConfigurationBuilder;
|
import org.acra.config.CoreConfigurationBuilder;
|
||||||
import org.schabi.newpipe.error.AppLifecycleObserver;
|
|
||||||
import org.schabi.newpipe.error.ReCaptchaActivity;
|
import org.schabi.newpipe.error.ReCaptchaActivity;
|
||||||
import org.schabi.newpipe.extractor.NewPipe;
|
import org.schabi.newpipe.extractor.NewPipe;
|
||||||
import org.schabi.newpipe.extractor.downloader.Downloader;
|
import org.schabi.newpipe.extractor.downloader.Downloader;
|
||||||
@ -81,9 +79,6 @@ public class App extends Application {
|
|||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
// Initialize the AppLifecycleObserver
|
|
||||||
AppLifecycleObserver.INSTANCE.initialize(this);
|
|
||||||
ProcessLifecycleOwner.get().getLifecycle().addObserver(AppLifecycleObserver.INSTANCE);
|
|
||||||
|
|
||||||
app = this;
|
app = this;
|
||||||
|
|
||||||
|
@ -123,7 +123,10 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
private static final int ITEM_ID_ABOUT = 1;
|
private static final int ITEM_ID_ABOUT = 1;
|
||||||
|
|
||||||
private static final int ORDER = 0;
|
private static final int ORDER = 0;
|
||||||
|
public static final String KEY_IS_IN_BACKGROUND = "is_in_background";
|
||||||
|
|
||||||
|
private SharedPreferences sharedPreferences;
|
||||||
|
private SharedPreferences.Editor sharedPrefEditor;
|
||||||
/*//////////////////////////////////////////////////////////////////////////
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
// Activity's LifeCycle
|
// Activity's LifeCycle
|
||||||
//////////////////////////////////////////////////////////////////////////*/
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
@ -140,6 +143,8 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
assureCorrectAppLanguage(this);
|
assureCorrectAppLanguage(this);
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||||
|
sharedPrefEditor = sharedPreferences.edit();
|
||||||
|
|
||||||
mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
|
mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
|
||||||
drawerLayoutBinding = mainBinding.drawerLayout;
|
drawerLayoutBinding = mainBinding.drawerLayout;
|
||||||
@ -181,16 +186,29 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
super.onPostCreate(savedInstanceState);
|
super.onPostCreate(savedInstanceState);
|
||||||
|
|
||||||
final App app = App.getApp();
|
final App app = App.getApp();
|
||||||
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(app);
|
|
||||||
|
|
||||||
if (prefs.getBoolean(app.getString(R.string.update_app_key), false)
|
if (sharedPreferences.getBoolean(app.getString(R.string.update_app_key), false)
|
||||||
&& prefs.getBoolean(app.getString(R.string.update_check_consent_key), false)) {
|
&& sharedPreferences
|
||||||
|
.getBoolean(app.getString(R.string.update_check_consent_key), false)) {
|
||||||
// Start the worker which is checking all conditions
|
// Start the worker which is checking all conditions
|
||||||
// and eventually searching for a new version.
|
// and eventually searching for a new version.
|
||||||
NewVersionWorker.enqueueNewVersionCheckingWork(app, false);
|
NewVersionWorker.enqueueNewVersionCheckingWork(app, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStart() {
|
||||||
|
super.onStart();
|
||||||
|
sharedPrefEditor.putBoolean(KEY_IS_IN_BACKGROUND, false).apply();
|
||||||
|
Log.d(TAG, "App moved to foreground");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStop() {
|
||||||
|
super.onStop();
|
||||||
|
sharedPrefEditor.putBoolean(KEY_IS_IN_BACKGROUND, true).apply();
|
||||||
|
Log.d(TAG, "App moved to background");
|
||||||
|
}
|
||||||
private void setupDrawer() throws ExtractionException {
|
private void setupDrawer() throws ExtractionException {
|
||||||
addDrawerMenuForCurrentService();
|
addDrawerMenuForCurrentService();
|
||||||
|
|
||||||
@ -483,13 +501,11 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
ErrorUtil.showUiErrorSnackbar(this, "Setting up service toggle", e);
|
ErrorUtil.showUiErrorSnackbar(this, "Setting up service toggle", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
final SharedPreferences sharedPreferences =
|
|
||||||
PreferenceManager.getDefaultSharedPreferences(this);
|
|
||||||
if (sharedPreferences.getBoolean(Constants.KEY_THEME_CHANGE, false)) {
|
if (sharedPreferences.getBoolean(Constants.KEY_THEME_CHANGE, false)) {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.d(TAG, "Theme has changed, recreating activity...");
|
Log.d(TAG, "Theme has changed, recreating activity...");
|
||||||
}
|
}
|
||||||
sharedPreferences.edit().putBoolean(Constants.KEY_THEME_CHANGE, false).apply();
|
sharedPrefEditor.putBoolean(Constants.KEY_THEME_CHANGE, false).apply();
|
||||||
ActivityCompat.recreate(this);
|
ActivityCompat.recreate(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -497,7 +513,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.d(TAG, "main page has changed, recreating main fragment...");
|
Log.d(TAG, "main page has changed, recreating main fragment...");
|
||||||
}
|
}
|
||||||
sharedPreferences.edit().putBoolean(Constants.KEY_MAIN_PAGE_CHANGE, false).apply();
|
sharedPrefEditor.putBoolean(Constants.KEY_MAIN_PAGE_CHANGE, false).apply();
|
||||||
NavigationHelper.openMainActivity(this);
|
NavigationHelper.openMainActivity(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,42 +0,0 @@
|
|||||||
package org.schabi.newpipe.error
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.SharedPreferences
|
|
||||||
import android.util.Log
|
|
||||||
import androidx.lifecycle.DefaultLifecycleObserver
|
|
||||||
import androidx.lifecycle.LifecycleOwner
|
|
||||||
import androidx.preference.PreferenceManager
|
|
||||||
|
|
||||||
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) {
|
|
||||||
if (!this::sharedPreferences.isInitialized) {
|
|
||||||
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
|
|
||||||
editor = sharedPreferences.edit()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStart(owner: LifecycleOwner) {
|
|
||||||
editor.putBoolean(KEY_IS_IN_BACKGROUND, false).commit()
|
|
||||||
Log.d(TAG, "App moved to foreground")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onPause(owner: LifecycleOwner) {
|
|
||||||
editor.putBoolean(KEY_IS_IN_BACKGROUND, true).commit()
|
|
||||||
Log.d(TAG, "App moved to background")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns if the app is currently in the background
|
|
||||||
* or in case of a crash the state when the crash happened
|
|
||||||
*/
|
|
||||||
fun isInBackground(): Boolean {
|
|
||||||
return sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true)
|
|
||||||
}
|
|
||||||
}
|
|
@ -11,9 +11,10 @@ import androidx.core.app.NotificationCompat
|
|||||||
import androidx.core.app.NotificationManagerCompat
|
import androidx.core.app.NotificationManagerCompat
|
||||||
import androidx.core.app.PendingIntentCompat
|
import androidx.core.app.PendingIntentCompat
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
|
import androidx.preference.PreferenceManager
|
||||||
import com.google.android.material.snackbar.Snackbar
|
import com.google.android.material.snackbar.Snackbar
|
||||||
|
import org.schabi.newpipe.MainActivity
|
||||||
import org.schabi.newpipe.R
|
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
|
* 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 {
|
|||||||
* activity (since the workflow would be interrupted anyway in that case). So never use this
|
* activity (since the workflow would be interrupted anyway in that case). So never use this
|
||||||
* for background services.
|
* for background services.
|
||||||
*
|
*
|
||||||
* If the crashed while the app was in the background open a notification instead
|
* If the crashed occurred while the app was in the background open a notification instead
|
||||||
*
|
*
|
||||||
* @param context the context to use to start the new activity
|
* @param context the context to use to start the new activity
|
||||||
* @param errorInfo the error info to be reported
|
* @param errorInfo the error info to be reported
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun openActivity(context: Context, errorInfo: ErrorInfo) {
|
fun openActivity(context: Context, errorInfo: ErrorInfo) {
|
||||||
if (isInBackground()) {
|
if (PreferenceManager.getDefaultSharedPreferences(context)
|
||||||
|
.getBoolean(MainActivity.KEY_IS_IN_BACKGROUND, true)
|
||||||
|
) {
|
||||||
createNotification(context, errorInfo)
|
createNotification(context, errorInfo)
|
||||||
} else {
|
} else {
|
||||||
context.startActivity(getErrorActivityIntent(context, errorInfo))
|
context.startActivity(getErrorActivityIntent(context, errorInfo))
|
||||||
|
Loading…
Reference in New Issue
Block a user