2016-01-01 21:16:41 +00:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
2018-11-18 13:45:50 +00:00
|
|
|
import android.annotation.TargetApi;
|
2016-01-01 21:16:41 +00:00
|
|
|
import android.app.Application;
|
2017-08-18 12:05:31 +00:00
|
|
|
import android.app.NotificationChannel;
|
|
|
|
import android.app.NotificationManager;
|
2016-01-01 22:04:29 +00:00
|
|
|
import android.content.Context;
|
2017-08-18 12:05:31 +00:00
|
|
|
import android.os.Build;
|
2017-09-03 06:04:18 +00:00
|
|
|
import android.util.Log;
|
2016-01-01 21:16:41 +00:00
|
|
|
|
2019-10-04 12:59:08 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2016-01-01 21:16:41 +00:00
|
|
|
|
2018-01-30 04:42:52 +00:00
|
|
|
import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache;
|
2016-02-05 16:09:29 +00:00
|
|
|
import com.nostra13.universalimageloader.core.ImageLoader;
|
|
|
|
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
|
2018-02-08 20:46:54 +00:00
|
|
|
import com.squareup.leakcanary.LeakCanary;
|
|
|
|
import com.squareup.leakcanary.RefWatcher;
|
2016-02-05 16:09:29 +00:00
|
|
|
|
2016-09-13 20:36:47 +00:00
|
|
|
import org.acra.ACRA;
|
|
|
|
import org.acra.config.ACRAConfiguration;
|
|
|
|
import org.acra.config.ACRAConfigurationException;
|
|
|
|
import org.acra.config.ConfigurationBuilder;
|
|
|
|
import org.acra.sender.ReportSenderFactory;
|
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;
|
2016-09-13 21:39:32 +00:00
|
|
|
import org.schabi.newpipe.report.AcraReportSenderFactory;
|
|
|
|
import org.schabi.newpipe.report.ErrorActivity;
|
2017-06-28 05:27:32 +00:00
|
|
|
import org.schabi.newpipe.report.UserAction;
|
2016-08-02 19:17:54 +00:00
|
|
|
import org.schabi.newpipe.settings.SettingsActivity;
|
2017-09-03 06:04:18 +00:00
|
|
|
import org.schabi.newpipe.util.ExtractorHelper;
|
2019-10-28 02:35:51 +00:00
|
|
|
import org.schabi.newpipe.util.Localization;
|
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
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InterruptedIOException;
|
|
|
|
import java.net.SocketException;
|
2018-02-21 06:35:25 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2016-01-01 21:16:41 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
import io.reactivex.annotations.NonNull;
|
|
|
|
import io.reactivex.exceptions.CompositeException;
|
2018-02-21 06:35:25 +00:00
|
|
|
import io.reactivex.exceptions.MissingBackpressureException;
|
|
|
|
import io.reactivex.exceptions.OnErrorNotImplementedException;
|
2017-09-03 06:04:18 +00:00
|
|
|
import io.reactivex.exceptions.UndeliverableException;
|
|
|
|
import io.reactivex.functions.Consumer;
|
|
|
|
import io.reactivex.plugins.RxJavaPlugins;
|
|
|
|
|
|
|
|
/*
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2016-01-01 21:16:41 +00:00
|
|
|
public class App extends Application {
|
2017-09-03 06:04:18 +00:00
|
|
|
protected static final String TAG = App.class.toString();
|
2018-02-08 20:46:54 +00:00
|
|
|
private RefWatcher refWatcher;
|
2018-11-18 13:45:50 +00:00
|
|
|
private static App app;
|
2017-09-03 06:04:18 +00:00
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2018-10-05 14:19:21 +00:00
|
|
|
private static final Class<? extends ReportSenderFactory>[]
|
|
|
|
reportSenderFactoryClasses = new Class[]{AcraReportSenderFactory.class};
|
2016-01-01 21:16:41 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
@Override
|
|
|
|
protected void attachBaseContext(Context base) {
|
|
|
|
super.attachBaseContext(base);
|
2016-01-01 22:04:29 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
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-02-08 20:46:54 +00:00
|
|
|
if (LeakCanary.isInAnalyzerProcess(this)) {
|
|
|
|
// This process is dedicated to LeakCanary for heap analysis.
|
|
|
|
// You should not init your app in this process.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
refWatcher = installLeakCanary();
|
|
|
|
|
2018-11-18 13:45:50 +00:00
|
|
|
app = this;
|
2018-08-12 09:31:50 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
// Initialize settings first because others inits can use its values
|
|
|
|
SettingsActivity.initSettings(this);
|
2017-08-07 13:02:30 +00:00
|
|
|
|
2018-10-05 14:19:21 +00:00
|
|
|
NewPipe.init(getDownloader(),
|
2019-10-28 02:35:51 +00:00
|
|
|
Localization.getPreferredLocalization(this),
|
|
|
|
Localization.getPreferredContentCountry(this));
|
2020-01-09 14:51:41 +00:00
|
|
|
Localization.init(getApplicationContext());
|
2019-10-28 02:35:51 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
StateSaver.init(this);
|
|
|
|
initNotificationChannel();
|
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
|
2018-02-03 17:36:40 +00:00
|
|
|
ImageLoader.getInstance().init(getImageLoaderConfigurations(10, 50));
|
2016-02-05 16:09:29 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
configureRxJavaErrorHandler();
|
2018-08-12 11:05:53 +00:00
|
|
|
|
|
|
|
// Check for new version
|
2018-08-12 13:04:20 +00:00
|
|
|
new CheckForNewAppVersionTask().execute();
|
2016-01-01 21:16:41 +00:00
|
|
|
}
|
|
|
|
|
2018-02-21 11:05:23 +00:00
|
|
|
protected Downloader getDownloader() {
|
2019-10-28 02:35:51 +00:00
|
|
|
return DownloaderImpl.init(null);
|
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
|
2018-08-28 17:17:14 +00:00
|
|
|
public void accept(@NonNull Throwable throwable) {
|
2018-02-21 06:35:25 +00:00
|
|
|
Log.e(TAG, "RxJavaPlugins.ErrorHandler called with -> : " +
|
|
|
|
"throwable = [" + throwable.getClass().getName() + "]");
|
2017-09-03 06:04:18 +00:00
|
|
|
|
|
|
|
if (throwable instanceof UndeliverableException) {
|
|
|
|
// As UndeliverableException is a wrapper, get the cause of it to get the "real" exception
|
|
|
|
throwable = throwable.getCause();
|
|
|
|
}
|
|
|
|
|
2018-02-21 06:35:25 +00:00
|
|
|
final List<Throwable> errors;
|
2017-09-03 06:04:18 +00:00
|
|
|
if (throwable instanceof CompositeException) {
|
2018-02-21 06:35:25 +00:00
|
|
|
errors = ((CompositeException) throwable).getExceptions();
|
|
|
|
} else {
|
|
|
|
errors = Collections.singletonList(throwable);
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
2018-02-21 06:35:25 +00:00
|
|
|
for (final Throwable error : errors) {
|
|
|
|
if (isThrowableIgnored(error)) return;
|
|
|
|
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()) {
|
|
|
|
reportException(throwable);
|
|
|
|
} else {
|
|
|
|
Log.e(TAG, "RxJavaPlugin: Undeliverable Exception received: ", throwable);
|
|
|
|
}
|
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
|
|
|
|
return ExtractorHelper.hasAssignableCauseThrowable(throwable,
|
2018-02-21 06:35:25 +00:00
|
|
|
IOException.class, SocketException.class, // network api cancellation
|
|
|
|
InterruptedException.class, InterruptedIOException.class); // blocking code disposed
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isThrowableCritical(@NonNull final Throwable throwable) {
|
|
|
|
// Though these exceptions cannot be ignored
|
|
|
|
return ExtractorHelper.hasAssignableCauseThrowable(throwable,
|
|
|
|
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
|
|
|
|
2018-02-03 17:36:40 +00:00
|
|
|
private ImageLoaderConfiguration getImageLoaderConfigurations(final int memoryCacheSizeMb,
|
|
|
|
final int diskCacheSizeMb) {
|
2018-01-30 04:42:52 +00:00
|
|
|
return new ImageLoaderConfiguration.Builder(this)
|
|
|
|
.memoryCache(new LRULimitedMemoryCache(memoryCacheSizeMb * 1024 * 1024))
|
2018-02-03 17:36:40 +00:00
|
|
|
.diskCacheSize(diskCacheSizeMb * 1024 * 1024)
|
2018-02-22 12:25:56 +00:00
|
|
|
.imageDownloader(new ImageDownloader(getApplicationContext()))
|
2018-01-30 04:42:52 +00:00
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
private void initACRA() {
|
|
|
|
try {
|
|
|
|
final ACRAConfiguration acraConfig = new ConfigurationBuilder(this)
|
|
|
|
.setReportSenderFactoryClasses(reportSenderFactoryClasses)
|
|
|
|
.setBuildConfigClass(BuildConfig.class)
|
|
|
|
.build();
|
|
|
|
ACRA.init(this, acraConfig);
|
|
|
|
} catch (ACRAConfigurationException ace) {
|
|
|
|
ace.printStackTrace();
|
2018-10-05 14:19:21 +00:00
|
|
|
ErrorActivity.reportError(this,
|
|
|
|
ace,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
ErrorActivity.ErrorInfo.make(UserAction.SOMETHING_ELSE, "none",
|
2017-09-03 06:04:18 +00:00
|
|
|
"Could not initialize ACRA crash report", R.string.app_ui_crash));
|
|
|
|
}
|
2016-01-02 21:47:21 +00:00
|
|
|
}
|
2017-08-18 12:05:31 +00:00
|
|
|
|
|
|
|
public void initNotificationChannel() {
|
|
|
|
if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String id = getString(R.string.notification_channel_id);
|
|
|
|
final CharSequence name = getString(R.string.notification_channel_name);
|
|
|
|
final String description = getString(R.string.notification_channel_description);
|
|
|
|
|
|
|
|
// Keep this below DEFAULT to avoid making noise on every notification update
|
|
|
|
final int importance = NotificationManager.IMPORTANCE_LOW;
|
|
|
|
|
|
|
|
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
|
|
|
|
mChannel.setDescription(description);
|
|
|
|
|
2018-10-05 14:19:21 +00:00
|
|
|
NotificationManager mNotificationManager =
|
|
|
|
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
2017-08-18 12:05:31 +00:00
|
|
|
mNotificationManager.createNotificationChannel(mChannel);
|
2018-08-12 09:31:50 +00:00
|
|
|
|
2018-11-18 13:45:50 +00:00
|
|
|
setUpUpdateNotificationChannel(importance);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up notification channel for app update.
|
|
|
|
* @param importance
|
|
|
|
*/
|
|
|
|
@TargetApi(Build.VERSION_CODES.O)
|
|
|
|
private void setUpUpdateNotificationChannel(int importance) {
|
|
|
|
|
2018-09-15 15:21:17 +00:00
|
|
|
final String appUpdateId
|
|
|
|
= getString(R.string.app_update_notification_channel_id);
|
|
|
|
final CharSequence appUpdateName
|
|
|
|
= getString(R.string.app_update_notification_channel_name);
|
|
|
|
final String appUpdateDescription
|
|
|
|
= getString(R.string.app_update_notification_channel_description);
|
|
|
|
|
|
|
|
NotificationChannel appUpdateChannel
|
|
|
|
= new NotificationChannel(appUpdateId, appUpdateName, importance);
|
|
|
|
appUpdateChannel.setDescription(appUpdateDescription);
|
|
|
|
|
|
|
|
NotificationManager appUpdateNotificationManager
|
|
|
|
= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
appUpdateNotificationManager.createNotificationChannel(appUpdateChannel);
|
2017-08-18 12:05:31 +00:00
|
|
|
}
|
2018-01-28 20:03:01 +00:00
|
|
|
|
2018-02-10 19:07:17 +00:00
|
|
|
@Nullable
|
2018-02-08 20:46:54 +00:00
|
|
|
public static RefWatcher getRefWatcher(Context context) {
|
|
|
|
final App application = (App) context.getApplicationContext();
|
|
|
|
return application.refWatcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected RefWatcher installLeakCanary() {
|
|
|
|
return RefWatcher.DISABLED;
|
|
|
|
}
|
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
|
|
|
}
|
2018-08-12 09:31:50 +00:00
|
|
|
|
2018-11-18 13:45:50 +00:00
|
|
|
public static App getApp() {
|
|
|
|
return app;
|
2018-08-12 09:31:50 +00:00
|
|
|
}
|
2016-01-01 21:16:41 +00:00
|
|
|
}
|