2016-01-01 21:16:41 +00:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
2016-01-01 22:04:29 +00:00
|
|
|
import android.content.Context;
|
2020-04-11 09:51:40 +00:00
|
|
|
import android.content.SharedPreferences;
|
2017-09-03 06:04:18 +00:00
|
|
|
import android.util.Log;
|
2021-01-15 16:11:04 +00:00
|
|
|
|
2020-07-14 19:15:29 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2021-07-10 09:17:41 +00:00
|
|
|
import androidx.core.app.NotificationChannelCompat;
|
|
|
|
import androidx.core.app.NotificationManagerCompat;
|
2020-10-18 08:44:27 +00:00
|
|
|
import androidx.multidex.MultiDexApplication;
|
2020-04-11 09:51:40 +00:00
|
|
|
import androidx.preference.PreferenceManager;
|
2021-01-15 16:11:04 +00:00
|
|
|
|
2021-09-06 18:47:44 +00:00
|
|
|
import com.jakewharton.processphoenix.ProcessPhoenix;
|
|
|
|
|
2016-09-13 20:36:47 +00:00
|
|
|
import org.acra.ACRA;
|
|
|
|
import org.acra.config.ACRAConfigurationException;
|
2020-05-01 16:26:12 +00:00
|
|
|
import org.acra.config.CoreConfiguration;
|
|
|
|
import org.acra.config.CoreConfigurationBuilder;
|
2020-12-09 11:42:01 +00:00
|
|
|
import org.schabi.newpipe.error.ErrorInfo;
|
2021-12-01 08:43:24 +00:00
|
|
|
import org.schabi.newpipe.error.ErrorUtil;
|
2020-12-09 11:42:01 +00:00
|
|
|
import org.schabi.newpipe.error.ReCaptchaActivity;
|
|
|
|
import org.schabi.newpipe.error.UserAction;
|
2016-09-27 20:59:04 +00:00
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
2019-10-28 02:35:51 +00:00
|
|
|
import org.schabi.newpipe.extractor.downloader.Downloader;
|
2020-11-21 09:45:42 +00:00
|
|
|
import org.schabi.newpipe.ktx.ExceptionUtils;
|
2020-06-13 15:29:57 +00:00
|
|
|
import org.schabi.newpipe.settings.NewPipeSettings;
|
2019-10-28 02:35:51 +00:00
|
|
|
import org.schabi.newpipe.util.Localization;
|
2021-03-27 13:37:44 +00:00
|
|
|
import org.schabi.newpipe.util.PicassoHelper;
|
2019-03-10 12:00:21 +00:00
|
|
|
import org.schabi.newpipe.util.ServiceHelper;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.util.StateSaver;
|
2016-08-02 19:17:54 +00:00
|
|
|
|
2021-01-15 16:11:04 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InterruptedIOException;
|
|
|
|
import java.net.SocketException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import io.reactivex.rxjava3.exceptions.CompositeException;
|
|
|
|
import io.reactivex.rxjava3.exceptions.MissingBackpressureException;
|
|
|
|
import io.reactivex.rxjava3.exceptions.OnErrorNotImplementedException;
|
|
|
|
import io.reactivex.rxjava3.exceptions.UndeliverableException;
|
|
|
|
import io.reactivex.rxjava3.functions.Consumer;
|
|
|
|
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
/*
|
2016-01-05 20:41:55 +00:00
|
|
|
* Copyright (C) Hans-Christoph Steiner 2016 <hans@eds.org>
|
|
|
|
* App.java is part of NewPipe.
|
|
|
|
*
|
|
|
|
* NewPipe is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* NewPipe is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-10-18 08:44:27 +00:00
|
|
|
public class App extends MultiDexApplication {
|
2020-12-19 13:48:03 +00:00
|
|
|
public static final String PACKAGE_NAME = BuildConfig.APPLICATION_ID;
|
2021-03-27 13:37:44 +00:00
|
|
|
private static final String TAG = App.class.toString();
|
|
|
|
private static App app;
|
2020-03-31 17:20:15 +00:00
|
|
|
|
2020-10-12 00:55:35 +00:00
|
|
|
@NonNull
|
2020-03-31 17:20:15 +00:00
|
|
|
public static App getApp() {
|
|
|
|
return app;
|
|
|
|
}
|
2016-01-01 21:16:41 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
protected void attachBaseContext(final Context base) {
|
2017-09-03 06:04:18 +00:00
|
|
|
super.attachBaseContext(base);
|
|
|
|
initACRA();
|
|
|
|
}
|
2016-09-13 20:36:47 +00:00
|
|
|
|
2016-01-01 21:16:41 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
2016-12-29 19:19:39 +00:00
|
|
|
|
2018-11-18 13:45:50 +00:00
|
|
|
app = this;
|
2018-08-12 09:31:50 +00:00
|
|
|
|
2021-09-06 18:47:44 +00:00
|
|
|
if (ProcessPhoenix.isPhoenixProcess(this)) {
|
|
|
|
Log.i(TAG, "This is a phoenix process! "
|
|
|
|
+ "Aborting initialization of App[onCreate]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
// Initialize settings first because others inits can use its values
|
2020-06-13 15:29:57 +00:00
|
|
|
NewPipeSettings.initSettings(this);
|
2017-08-07 13:02:30 +00:00
|
|
|
|
2018-10-05 14:19:21 +00:00
|
|
|
NewPipe.init(getDownloader(),
|
2021-01-06 13:48:34 +00:00
|
|
|
Localization.getPreferredLocalization(this),
|
|
|
|
Localization.getPreferredContentCountry(this));
|
|
|
|
Localization.initPrettyTime(Localization.resolvePrettyTime(getApplicationContext()));
|
2019-10-28 02:35:51 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
StateSaver.init(this);
|
2020-09-16 13:33:15 +00:00
|
|
|
initNotificationChannels();
|
2016-09-27 20:59:04 +00:00
|
|
|
|
2019-03-10 12:00:21 +00:00
|
|
|
ServiceHelper.initServices(this);
|
|
|
|
|
2016-02-05 16:09:29 +00:00
|
|
|
// Initialize image loader
|
2021-03-27 13:59:24 +00:00
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2021-03-27 13:37:44 +00:00
|
|
|
PicassoHelper.init(this);
|
2021-03-27 13:59:24 +00:00
|
|
|
PicassoHelper.setShouldLoadImages(
|
|
|
|
prefs.getBoolean(getString(R.string.download_thumbnail_key), true));
|
2021-06-07 09:24:13 +00:00
|
|
|
PicassoHelper.setIndicatorsEnabled(MainActivity.DEBUG
|
2021-03-27 13:59:24 +00:00
|
|
|
&& prefs.getBoolean(getString(R.string.show_image_indicators_key), false));
|
2016-02-05 16:09:29 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
configureRxJavaErrorHandler();
|
2020-10-12 00:55:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTerminate() {
|
|
|
|
super.onTerminate();
|
2021-03-27 13:37:44 +00:00
|
|
|
PicassoHelper.terminate();
|
2016-01-01 21:16:41 +00:00
|
|
|
}
|
|
|
|
|
2018-02-21 11:05:23 +00:00
|
|
|
protected Downloader getDownloader() {
|
2020-08-16 08:24:58 +00:00
|
|
|
final DownloaderImpl downloader = DownloaderImpl.init(null);
|
2020-04-11 09:51:40 +00:00
|
|
|
setCookiesToDownloader(downloader);
|
|
|
|
return downloader;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void setCookiesToDownloader(final DownloaderImpl downloader) {
|
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
|
|
|
|
getApplicationContext());
|
|
|
|
final String key = getApplicationContext().getString(R.string.recaptcha_cookies_key);
|
2021-04-04 10:26:55 +00:00
|
|
|
downloader.setCookie(ReCaptchaActivity.RECAPTCHA_COOKIES_KEY, prefs.getString(key, null));
|
2020-04-12 20:13:04 +00:00
|
|
|
downloader.updateYoutubeRestrictedModeCookies(getApplicationContext());
|
2018-02-21 11:05:23 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
private void configureRxJavaErrorHandler() {
|
|
|
|
// https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling
|
|
|
|
RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public void accept(@NonNull final Throwable throwable) {
|
|
|
|
Log.e(TAG, "RxJavaPlugins.ErrorHandler called with -> : "
|
|
|
|
+ "throwable = [" + throwable.getClass().getName() + "]");
|
2017-09-03 06:04:18 +00:00
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
final Throwable actualThrowable;
|
2017-09-03 06:04:18 +00:00
|
|
|
if (throwable instanceof UndeliverableException) {
|
2020-03-31 17:20:15 +00:00
|
|
|
// As UndeliverableException is a wrapper,
|
|
|
|
// get the cause of it to get the "real" exception
|
|
|
|
actualThrowable = throwable.getCause();
|
|
|
|
} else {
|
|
|
|
actualThrowable = throwable;
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
2018-02-21 06:35:25 +00:00
|
|
|
final List<Throwable> errors;
|
2020-03-31 17:20:15 +00:00
|
|
|
if (actualThrowable instanceof CompositeException) {
|
|
|
|
errors = ((CompositeException) actualThrowable).getExceptions();
|
2018-02-21 06:35:25 +00:00
|
|
|
} else {
|
2020-03-31 17:20:15 +00:00
|
|
|
errors = Collections.singletonList(actualThrowable);
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
2018-02-21 06:35:25 +00:00
|
|
|
for (final Throwable error : errors) {
|
2020-03-31 17:20:15 +00:00
|
|
|
if (isThrowableIgnored(error)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-21 06:35:25 +00:00
|
|
|
if (isThrowableCritical(error)) {
|
|
|
|
reportException(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
|
2018-02-21 06:35:25 +00:00
|
|
|
// Out-of-lifecycle exceptions should only be reported if a debug user wishes so,
|
|
|
|
// When exception is not reported, log it
|
|
|
|
if (isDisposedRxExceptionsReported()) {
|
2020-03-31 17:20:15 +00:00
|
|
|
reportException(actualThrowable);
|
2018-02-21 06:35:25 +00:00
|
|
|
} else {
|
2020-03-31 17:20:15 +00:00
|
|
|
Log.e(TAG, "RxJavaPlugin: Undeliverable Exception received: ", actualThrowable);
|
2018-02-21 06:35:25 +00:00
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
2018-02-21 06:35:25 +00:00
|
|
|
private boolean isThrowableIgnored(@NonNull final Throwable throwable) {
|
2017-09-03 06:04:18 +00:00
|
|
|
// Don't crash the application over a simple network problem
|
2020-03-28 23:08:42 +00:00
|
|
|
return ExceptionUtils.hasAssignableCause(throwable,
|
2020-03-31 17:20:15 +00:00
|
|
|
// network api cancellation
|
|
|
|
IOException.class, SocketException.class,
|
|
|
|
// blocking code disposed
|
|
|
|
InterruptedException.class, InterruptedIOException.class);
|
2018-02-21 06:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isThrowableCritical(@NonNull final Throwable throwable) {
|
|
|
|
// Though these exceptions cannot be ignored
|
2020-03-28 23:08:42 +00:00
|
|
|
return ExceptionUtils.hasAssignableCause(throwable,
|
2018-02-21 06:35:25 +00:00
|
|
|
NullPointerException.class, IllegalArgumentException.class, // bug in app
|
|
|
|
OnErrorNotImplementedException.class, MissingBackpressureException.class,
|
|
|
|
IllegalStateException.class); // bug in operator
|
|
|
|
}
|
|
|
|
|
|
|
|
private void reportException(@NonNull final Throwable throwable) {
|
|
|
|
// Throw uncaught exception that will trigger the report system
|
|
|
|
Thread.currentThread().getUncaughtExceptionHandler()
|
|
|
|
.uncaughtException(Thread.currentThread(), throwable);
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
});
|
2016-01-01 21:16:41 +00:00
|
|
|
}
|
2016-01-01 22:04:29 +00:00
|
|
|
|
2020-07-28 08:45:48 +00:00
|
|
|
/**
|
|
|
|
* Called in {@link #attachBaseContext(Context)} after calling the {@code super} method.
|
|
|
|
* Should be overridden if MultiDex is enabled, since it has to be initialized before ACRA.
|
|
|
|
*/
|
|
|
|
protected void initACRA() {
|
2020-07-28 08:48:54 +00:00
|
|
|
if (ACRA.isACRASenderServiceProcess()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
try {
|
2020-05-01 16:26:12 +00:00
|
|
|
final CoreConfiguration acraConfig = new CoreConfigurationBuilder(this)
|
2017-09-03 06:04:18 +00:00
|
|
|
.setBuildConfigClass(BuildConfig.class)
|
|
|
|
.build();
|
|
|
|
ACRA.init(this, acraConfig);
|
2020-12-11 13:55:47 +00:00
|
|
|
} catch (final ACRAConfigurationException exception) {
|
|
|
|
exception.printStackTrace();
|
2021-12-01 08:43:24 +00:00
|
|
|
ErrorUtil.openActivity(this, new ErrorInfo(exception,
|
2020-12-11 13:55:47 +00:00
|
|
|
UserAction.SOMETHING_ELSE, "Could not initialize ACRA crash report"));
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
2016-01-02 21:47:21 +00:00
|
|
|
}
|
2017-08-18 12:05:31 +00:00
|
|
|
|
2020-09-16 13:33:15 +00:00
|
|
|
private void initNotificationChannels() {
|
2021-07-10 09:17:41 +00:00
|
|
|
// Keep the importance below DEFAULT to avoid making noise on every notification update for
|
|
|
|
// the main and update channels
|
|
|
|
final NotificationChannelCompat mainChannel = new NotificationChannelCompat
|
|
|
|
.Builder(getString(R.string.notification_channel_id),
|
2021-12-01 08:10:59 +00:00
|
|
|
NotificationManagerCompat.IMPORTANCE_LOW)
|
2021-07-10 09:17:41 +00:00
|
|
|
.setName(getString(R.string.notification_channel_name))
|
|
|
|
.setDescription(getString(R.string.notification_channel_description))
|
|
|
|
.build();
|
2018-11-18 13:45:50 +00:00
|
|
|
|
2021-07-10 09:17:41 +00:00
|
|
|
final NotificationChannelCompat appUpdateChannel = new NotificationChannelCompat
|
|
|
|
.Builder(getString(R.string.app_update_notification_channel_id),
|
2021-12-01 08:10:59 +00:00
|
|
|
NotificationManagerCompat.IMPORTANCE_LOW)
|
2021-07-10 09:17:41 +00:00
|
|
|
.setName(getString(R.string.app_update_notification_channel_name))
|
|
|
|
.setDescription(getString(R.string.app_update_notification_channel_description))
|
|
|
|
.build();
|
2020-12-11 01:59:24 +00:00
|
|
|
|
2021-07-10 09:17:41 +00:00
|
|
|
final NotificationChannelCompat hashChannel = new NotificationChannelCompat
|
|
|
|
.Builder(getString(R.string.hash_channel_id),
|
2021-12-01 08:10:59 +00:00
|
|
|
NotificationManagerCompat.IMPORTANCE_HIGH)
|
2021-07-10 09:17:41 +00:00
|
|
|
.setName(getString(R.string.hash_channel_name))
|
|
|
|
.setDescription(getString(R.string.hash_channel_description))
|
|
|
|
.build();
|
2020-12-11 01:59:24 +00:00
|
|
|
|
2021-12-01 08:10:59 +00:00
|
|
|
final NotificationChannelCompat errorReportChannel = new NotificationChannelCompat
|
|
|
|
.Builder(getString(R.string.error_report_channel_id),
|
|
|
|
NotificationManagerCompat.IMPORTANCE_LOW)
|
|
|
|
.setName(getString(R.string.error_report_channel_name))
|
|
|
|
.setDescription(getString(R.string.error_report_channel_description))
|
|
|
|
.build();
|
|
|
|
|
2021-07-10 09:17:41 +00:00
|
|
|
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
|
|
|
|
notificationManager.createNotificationChannelsCompat(Arrays.asList(mainChannel,
|
2021-12-01 08:10:59 +00:00
|
|
|
appUpdateChannel, hashChannel, errorReportChannel));
|
2017-08-18 12:05:31 +00:00
|
|
|
}
|
2018-01-28 20:03:01 +00:00
|
|
|
|
2018-02-21 06:35:25 +00:00
|
|
|
protected boolean isDisposedRxExceptionsReported() {
|
2018-02-21 18:42:54 +00:00
|
|
|
return false;
|
2018-02-21 06:35:25 +00:00
|
|
|
}
|
2021-10-17 10:54:56 +00:00
|
|
|
|
2016-01-01 21:16:41 +00:00
|
|
|
}
|