2016-01-01 22:16:41 +01:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
2017-08-18 14:05:31 +02:00
|
|
|
import android.app.NotificationChannel;
|
|
|
|
import android.app.NotificationManager;
|
2016-01-01 23:04:29 +01:00
|
|
|
import android.content.Context;
|
2020-04-11 11:51:40 +02:00
|
|
|
import android.content.SharedPreferences;
|
2017-08-18 14:05:31 +02:00
|
|
|
import android.os.Build;
|
2017-09-03 08:04:18 +02:00
|
|
|
import android.util.Log;
|
2020-07-14 21:15:29 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2020-11-21 11:38:08 +01:00
|
|
|
import androidx.annotation.Nullable;
|
2020-10-18 10:44:27 +02:00
|
|
|
import androidx.multidex.MultiDexApplication;
|
2020-04-11 11:51:40 +02:00
|
|
|
import androidx.preference.PreferenceManager;
|
2018-01-30 05:42:52 +01:00
|
|
|
import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache;
|
2016-02-05 17:09:29 +01:00
|
|
|
import com.nostra13.universalimageloader.core.ImageLoader;
|
|
|
|
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
|
2021-01-06 14:48:34 +01:00
|
|
|
import io.reactivex.rxjava3.disposables.Disposable;
|
|
|
|
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;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InterruptedIOException;
|
|
|
|
import java.net.SocketException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2016-09-13 22:36:47 +02:00
|
|
|
import org.acra.ACRA;
|
|
|
|
import org.acra.config.ACRAConfigurationException;
|
2020-05-01 18:26:12 +02:00
|
|
|
import org.acra.config.CoreConfiguration;
|
|
|
|
import org.acra.config.CoreConfigurationBuilder;
|
2016-09-27 22:59:04 +02:00
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
2019-10-28 03:35:51 +01:00
|
|
|
import org.schabi.newpipe.extractor.downloader.Downloader;
|
2020-11-21 10:45:42 +01:00
|
|
|
import org.schabi.newpipe.ktx.ExceptionUtils;
|
2016-09-13 23:39:32 +02:00
|
|
|
import org.schabi.newpipe.report.ErrorActivity;
|
2020-10-04 03:50:28 +02:00
|
|
|
import org.schabi.newpipe.report.ErrorInfo;
|
2017-06-28 07:27:32 +02:00
|
|
|
import org.schabi.newpipe.report.UserAction;
|
2016-08-02 21:17:54 +02:00
|
|
|
import org.schabi.newpipe.settings.SettingsActivity;
|
2019-10-28 03:35:51 +01:00
|
|
|
import org.schabi.newpipe.util.Localization;
|
2019-03-10 13:00:21 +01:00
|
|
|
import org.schabi.newpipe.util.ServiceHelper;
|
2017-09-03 08:04:18 +02:00
|
|
|
import org.schabi.newpipe.util.StateSaver;
|
2016-08-02 21:17:54 +02:00
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
/*
|
2016-01-05 21:41:55 +01: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 10:44:27 +02:00
|
|
|
public class App extends MultiDexApplication {
|
2017-09-03 08:04:18 +02:00
|
|
|
protected static final String TAG = App.class.toString();
|
2020-03-31 19:20:15 +02:00
|
|
|
private static App app;
|
2020-12-19 14:48:03 +01:00
|
|
|
public static final String PACKAGE_NAME = BuildConfig.APPLICATION_ID;
|
2020-03-31 19:20:15 +02:00
|
|
|
|
2020-12-19 14:48:03 +01:00
|
|
|
@Nullable
|
|
|
|
private Disposable disposable = null;
|
2020-10-12 02:55:35 +02:00
|
|
|
|
|
|
|
@NonNull
|
2020-03-31 19:20:15 +02:00
|
|
|
public static App getApp() {
|
|
|
|
return app;
|
|
|
|
}
|
2016-01-01 22:16:41 +01:00
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
protected void attachBaseContext(final Context base) {
|
2017-09-03 08:04:18 +02:00
|
|
|
super.attachBaseContext(base);
|
|
|
|
initACRA();
|
|
|
|
}
|
2016-09-13 22:36:47 +02:00
|
|
|
|
2016-01-01 22:16:41 +01:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
2016-12-29 20:19:39 +01:00
|
|
|
|
2018-11-18 14:45:50 +01:00
|
|
|
app = this;
|
2018-08-12 11:31:50 +02:00
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
// Initialize settings first because others inits can use its values
|
|
|
|
SettingsActivity.initSettings(this);
|
2017-08-07 15:02:30 +02:00
|
|
|
|
2018-10-05 16:19:21 +02:00
|
|
|
NewPipe.init(getDownloader(),
|
2021-01-06 14:48:34 +01:00
|
|
|
Localization.getPreferredLocalization(this),
|
|
|
|
Localization.getPreferredContentCountry(this));
|
|
|
|
Localization.initPrettyTime(Localization.resolvePrettyTime(getApplicationContext()));
|
2019-10-28 03:35:51 +01:00
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
StateSaver.init(this);
|
2020-09-16 15:33:15 +02:00
|
|
|
initNotificationChannels();
|
2016-09-27 22:59:04 +02:00
|
|
|
|
2019-03-10 13:00:21 +01:00
|
|
|
ServiceHelper.initServices(this);
|
|
|
|
|
2016-02-05 17:09:29 +01:00
|
|
|
// Initialize image loader
|
2018-02-03 18:36:40 +01:00
|
|
|
ImageLoader.getInstance().init(getImageLoaderConfigurations(10, 50));
|
2016-02-05 17:09:29 +01:00
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
configureRxJavaErrorHandler();
|
2018-08-12 13:05:53 +02:00
|
|
|
|
|
|
|
// Check for new version
|
2020-10-12 02:55:35 +02:00
|
|
|
disposable = CheckForNewAppVersion.checkNewVersion(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTerminate() {
|
|
|
|
if (disposable != null) {
|
|
|
|
disposable.dispose();
|
|
|
|
}
|
|
|
|
super.onTerminate();
|
2016-01-01 22:16:41 +01:00
|
|
|
}
|
|
|
|
|
2018-02-21 12:05:23 +01:00
|
|
|
protected Downloader getDownloader() {
|
2020-08-16 10:24:58 +02:00
|
|
|
final DownloaderImpl downloader = DownloaderImpl.init(null);
|
2020-04-11 11:51:40 +02: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);
|
2020-03-15 22:53:29 +01:00
|
|
|
downloader.setCookie(ReCaptchaActivity.RECAPTCHA_COOKIES_KEY, prefs.getString(key, ""));
|
2020-04-12 22:13:04 +02:00
|
|
|
downloader.updateYoutubeRestrictedModeCookies(getApplicationContext());
|
2018-02-21 12:05:23 +01:00
|
|
|
}
|
|
|
|
|
2017-09-03 08:04:18 +02: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 19:20:15 +02:00
|
|
|
public void accept(@NonNull final Throwable throwable) {
|
|
|
|
Log.e(TAG, "RxJavaPlugins.ErrorHandler called with -> : "
|
|
|
|
+ "throwable = [" + throwable.getClass().getName() + "]");
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
final Throwable actualThrowable;
|
2017-09-03 08:04:18 +02:00
|
|
|
if (throwable instanceof UndeliverableException) {
|
2020-03-31 19:20:15 +02: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 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2018-02-21 07:35:25 +01:00
|
|
|
final List<Throwable> errors;
|
2020-03-31 19:20:15 +02:00
|
|
|
if (actualThrowable instanceof CompositeException) {
|
|
|
|
errors = ((CompositeException) actualThrowable).getExceptions();
|
2018-02-21 07:35:25 +01:00
|
|
|
} else {
|
2020-03-31 19:20:15 +02:00
|
|
|
errors = Collections.singletonList(actualThrowable);
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2018-02-21 07:35:25 +01:00
|
|
|
for (final Throwable error : errors) {
|
2020-03-31 19:20:15 +02:00
|
|
|
if (isThrowableIgnored(error)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-21 07:35:25 +01:00
|
|
|
if (isThrowableCritical(error)) {
|
|
|
|
reportException(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2018-02-21 07:35:25 +01: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 19:20:15 +02:00
|
|
|
reportException(actualThrowable);
|
2018-02-21 07:35:25 +01:00
|
|
|
} else {
|
2020-03-31 19:20:15 +02:00
|
|
|
Log.e(TAG, "RxJavaPlugin: Undeliverable Exception received: ", actualThrowable);
|
2018-02-21 07:35:25 +01:00
|
|
|
}
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
|
|
|
|
2018-02-21 07:35:25 +01:00
|
|
|
private boolean isThrowableIgnored(@NonNull final Throwable throwable) {
|
2017-09-03 08:04:18 +02:00
|
|
|
// Don't crash the application over a simple network problem
|
2020-03-29 00:08:42 +01:00
|
|
|
return ExceptionUtils.hasAssignableCause(throwable,
|
2020-03-31 19:20:15 +02:00
|
|
|
// network api cancellation
|
|
|
|
IOException.class, SocketException.class,
|
|
|
|
// blocking code disposed
|
|
|
|
InterruptedException.class, InterruptedIOException.class);
|
2018-02-21 07:35:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isThrowableCritical(@NonNull final Throwable throwable) {
|
|
|
|
// Though these exceptions cannot be ignored
|
2020-03-29 00:08:42 +01:00
|
|
|
return ExceptionUtils.hasAssignableCause(throwable,
|
2018-02-21 07:35:25 +01: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 08:04:18 +02:00
|
|
|
}
|
|
|
|
});
|
2016-01-01 22:16:41 +01:00
|
|
|
}
|
2016-01-01 23:04:29 +01:00
|
|
|
|
2018-02-03 18:36:40 +01:00
|
|
|
private ImageLoaderConfiguration getImageLoaderConfigurations(final int memoryCacheSizeMb,
|
|
|
|
final int diskCacheSizeMb) {
|
2018-01-30 05:42:52 +01:00
|
|
|
return new ImageLoaderConfiguration.Builder(this)
|
|
|
|
.memoryCache(new LRULimitedMemoryCache(memoryCacheSizeMb * 1024 * 1024))
|
2018-02-03 18:36:40 +01:00
|
|
|
.diskCacheSize(diskCacheSizeMb * 1024 * 1024)
|
2018-02-22 13:25:56 +01:00
|
|
|
.imageDownloader(new ImageDownloader(getApplicationContext()))
|
2018-01-30 05:42:52 +01:00
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2020-07-28 10:45:48 +02: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 10:48:54 +02:00
|
|
|
if (ACRA.isACRASenderServiceProcess()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
try {
|
2020-05-01 18:26:12 +02:00
|
|
|
final CoreConfiguration acraConfig = new CoreConfigurationBuilder(this)
|
2017-09-03 08:04:18 +02:00
|
|
|
.setBuildConfigClass(BuildConfig.class)
|
|
|
|
.build();
|
|
|
|
ACRA.init(this, acraConfig);
|
2020-08-16 10:24:58 +02:00
|
|
|
} catch (final ACRAConfigurationException ace) {
|
2017-09-03 08:04:18 +02:00
|
|
|
ace.printStackTrace();
|
2018-10-05 16:19:21 +02:00
|
|
|
ErrorActivity.reportError(this,
|
|
|
|
ace,
|
|
|
|
null,
|
|
|
|
null,
|
2020-10-04 03:50:28 +02:00
|
|
|
ErrorInfo.make(UserAction.SOMETHING_ELSE, "none",
|
2020-03-31 19:20:15 +02:00
|
|
|
"Could not initialize ACRA crash report", R.string.app_ui_crash));
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|
2016-01-02 22:47:21 +01:00
|
|
|
}
|
2017-08-18 14:05:31 +02:00
|
|
|
|
2020-09-16 15:33:15 +02:00
|
|
|
private void initNotificationChannels() {
|
2020-08-27 22:59:29 +02:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
2017-08-18 14:05:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-16 15:33:15 +02:00
|
|
|
String id = getString(R.string.notification_channel_id);
|
|
|
|
String name = getString(R.string.notification_channel_name);
|
|
|
|
String description = getString(R.string.notification_channel_description);
|
2017-08-18 14:05:31 +02:00
|
|
|
|
2020-12-11 02:59:24 +01:00
|
|
|
// Keep this below DEFAULT to avoid making noise on every notification update for the main
|
|
|
|
// and update channels
|
|
|
|
int importance = NotificationManager.IMPORTANCE_LOW;
|
2017-08-18 14:05:31 +02:00
|
|
|
|
2020-09-16 15:33:15 +02:00
|
|
|
final NotificationChannel mainChannel = new NotificationChannel(id, name, importance);
|
|
|
|
mainChannel.setDescription(description);
|
2017-08-18 14:05:31 +02:00
|
|
|
|
2020-09-16 15:33:15 +02:00
|
|
|
id = getString(R.string.app_update_notification_channel_id);
|
|
|
|
name = getString(R.string.app_update_notification_channel_name);
|
|
|
|
description = getString(R.string.app_update_notification_channel_description);
|
2018-08-12 11:31:50 +02:00
|
|
|
|
2020-09-16 15:33:15 +02:00
|
|
|
final NotificationChannel appUpdateChannel = new NotificationChannel(id, name, importance);
|
|
|
|
appUpdateChannel.setDescription(description);
|
2018-11-18 14:45:50 +01:00
|
|
|
|
2020-12-11 02:59:24 +01:00
|
|
|
id = getString(R.string.hash_channel_id);
|
|
|
|
name = getString(R.string.hash_channel_name);
|
|
|
|
description = getString(R.string.hash_channel_description);
|
|
|
|
importance = NotificationManager.IMPORTANCE_HIGH;
|
|
|
|
|
|
|
|
final NotificationChannel hashChannel = new NotificationChannel(id, name, importance);
|
|
|
|
hashChannel.setDescription(description);
|
|
|
|
|
2020-09-16 15:33:15 +02:00
|
|
|
final NotificationManager notificationManager = getSystemService(NotificationManager.class);
|
|
|
|
notificationManager.createNotificationChannels(Arrays.asList(mainChannel,
|
2020-12-11 02:59:24 +01:00
|
|
|
appUpdateChannel, hashChannel));
|
2017-08-18 14:05:31 +02:00
|
|
|
}
|
2018-01-28 21:03:01 +01:00
|
|
|
|
2018-02-21 07:35:25 +01:00
|
|
|
protected boolean isDisposedRxExceptionsReported() {
|
2018-02-21 19:42:54 +01:00
|
|
|
return false;
|
2018-02-21 07:35:25 +01:00
|
|
|
}
|
2016-01-01 22:16:41 +01:00
|
|
|
}
|