diff --git a/app/src/main/java/com/deniscerri/ytdlnis/App.java b/app/src/main/java/com/deniscerri/ytdlnis/App.java deleted file mode 100644 index 3ece600a..00000000 --- a/app/src/main/java/com/deniscerri/ytdlnis/App.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.deniscerri.ytdlnis; - -import android.app.Application; -import android.util.Log; -import android.widget.Toast; -import androidx.preference.PreferenceManager; -import com.deniscerri.ytdlnis.util.NotificationUtil; -import com.google.android.material.color.DynamicColors; -import com.yausername.aria2c.Aria2c; -import com.yausername.ffmpeg.FFmpeg; -import com.yausername.youtubedl_android.YoutubeDL; -import com.yausername.youtubedl_android.YoutubeDLException; -import io.reactivex.Completable; -import io.reactivex.android.schedulers.AndroidSchedulers; -import io.reactivex.exceptions.UndeliverableException; -import io.reactivex.observers.DisposableCompletableObserver; -import io.reactivex.plugins.RxJavaPlugins; -import io.reactivex.schedulers.Schedulers; - -public class App extends Application { - - private static final String TAG = "App"; - public static NotificationUtil notificationUtil; - - @Override - public void onCreate() { - super.onCreate(); - DynamicColors.applyToActivitiesIfAvailable(this); - notificationUtil = new NotificationUtil(this); - createNotificationChannels(); - PreferenceManager.setDefaultValues(this, "root_preferences", this.MODE_PRIVATE, R.xml.root_preferences, false); - configureRxJavaErrorHandler(); - Completable.fromAction(this::initLibraries).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new DisposableCompletableObserver() { - @Override - public void onComplete() { - // it worked - } - - @Override - public void onError(Throwable e) { - if(BuildConfig.DEBUG) Log.e(TAG, "failed to initialize youtubedl-android", e); - Toast.makeText(getApplicationContext(), "initialization failed: " + e.getLocalizedMessage(), Toast.LENGTH_SHORT).show(); - } - }); - } - - private void configureRxJavaErrorHandler() { - RxJavaPlugins.setErrorHandler(e -> { - - if (e instanceof UndeliverableException) { - // As UndeliverableException is a wrapper, get the cause of it to get the "real" exception - e = e.getCause(); - } - - if (e instanceof InterruptedException) { - // fine, some blocking code was interrupted by a dispose call - return; - } - - Log.e(TAG, "Undeliverable exception received, not sure what to do", e); - }); - } - - private void initLibraries() throws YoutubeDLException { - YoutubeDL.getInstance().init(this); - FFmpeg.getInstance().init(this); - Aria2c.getInstance().init(this); - } - - private void createNotificationChannels() { - notificationUtil.createNotificationChannel(); - } -} diff --git a/app/src/main/java/com/deniscerri/ytdlnis/App.kt b/app/src/main/java/com/deniscerri/ytdlnis/App.kt new file mode 100644 index 00000000..308bf2a9 --- /dev/null +++ b/app/src/main/java/com/deniscerri/ytdlnis/App.kt @@ -0,0 +1,84 @@ +package com.deniscerri.ytdlnis + +import android.annotation.SuppressLint +import android.app.Application +import android.util.Log +import android.widget.Toast +import androidx.preference.PreferenceManager +import com.deniscerri.ytdlnis.util.NotificationUtil +import com.google.android.material.color.DynamicColors +import com.yausername.aria2c.Aria2c +import com.yausername.ffmpeg.FFmpeg +import com.yausername.youtubedl_android.YoutubeDL +import com.yausername.youtubedl_android.YoutubeDLException +import io.reactivex.Completable +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.exceptions.UndeliverableException +import io.reactivex.observers.DisposableCompletableObserver +import io.reactivex.plugins.RxJavaPlugins +import io.reactivex.schedulers.Schedulers + +class App : Application() { + override fun onCreate() { + super.onCreate() + DynamicColors.applyToActivitiesIfAvailable(this) + notificationUtil = NotificationUtil(this) + createNotificationChannels() + PreferenceManager.setDefaultValues( + this, + "root_preferences", + MODE_PRIVATE, + R.xml.root_preferences, + false + ) + configureRxJavaErrorHandler() + Completable.fromAction { initLibraries() }.subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(object : DisposableCompletableObserver() { + override fun onComplete() { + // it worked + } + + override fun onError(e: Throwable) { + if (BuildConfig.DEBUG) Log.e(TAG, "failed to initialize youtubedl-android", e) + Toast.makeText( + applicationContext, + "initialization failed: " + e.localizedMessage, + Toast.LENGTH_SHORT + ).show() + } + }) + } + + private fun configureRxJavaErrorHandler() { + var err = Throwable() + RxJavaPlugins.setErrorHandler { e: Throwable -> + if (e is UndeliverableException) { + // As UndeliverableException is a wrapper, get the cause of it to get the "real" exception + err = e.cause!! + } + if (e is InterruptedException) { + // fine, some blocking code was interrupted by a dispose call + return@setErrorHandler + } + Log.e(TAG, "Undeliverable exception received, not sure what to do", err) + } + } + + @Throws(YoutubeDLException::class) + private fun initLibraries() { + YoutubeDL.getInstance().init(this) + FFmpeg.getInstance().init(this) + Aria2c.getInstance().init(this) + } + + private fun createNotificationChannels() { + notificationUtil.createNotificationChannel() + } + + companion object { + private const val TAG = "App" + @SuppressLint("StaticFieldLeak") + lateinit var notificationUtil: NotificationUtil + } +} \ No newline at end of file diff --git a/app/src/main/java/com/deniscerri/ytdlnis/MainActivity.kt b/app/src/main/java/com/deniscerri/ytdlnis/MainActivity.kt index 031341f6..2958abf1 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/MainActivity.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/MainActivity.kt @@ -79,6 +79,7 @@ class MainActivity : AppCompatActivity() { setContentView(R.layout.activity_main) setContentView(binding.root) context = baseContext + askPermissions() reconnectDownloadService() checkUpdate() fm = supportFragmentManager @@ -126,7 +127,6 @@ class MainActivity : AppCompatActivity() { view.onApplyWindowInsets(windowInsets) windowInsets } - askPermissions() val intent = intent handleIntents(intent) } diff --git a/app/src/main/java/com/deniscerri/ytdlnis/page/DownloadsFragment.java b/app/src/main/java/com/deniscerri/ytdlnis/page/DownloadsFragment.java index c58c5cc6..bd6f23e5 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/page/DownloadsFragment.java +++ b/app/src/main/java/com/deniscerri/ytdlnis/page/DownloadsFragment.java @@ -192,12 +192,11 @@ public class DownloadsFragment extends Fragment implements DownloadsRecyclerView Video v = info.getVideo(); v = findVideo(v.getURL(), v.getDownloadedType()); try { - downloadsObjects.remove(v); databaseManager = new DatabaseManager(context); databaseManager.clearHistoryItem(v,false); databaseManager.close(); downloadsRecyclerViewAdapter.notifyItemRemoved(downloadsObjects.indexOf(v)); - + downloadsObjects.remove(v); if (downloadsObjects.isEmpty()) initCards(); downloading = false; }catch (Exception ignored){} diff --git a/app/src/main/java/com/deniscerri/ytdlnis/receiver/NotificationReceiver.java b/app/src/main/java/com/deniscerri/ytdlnis/receiver/NotificationReceiver.java deleted file mode 100644 index 7f48b3c8..00000000 --- a/app/src/main/java/com/deniscerri/ytdlnis/receiver/NotificationReceiver.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.deniscerri.ytdlnis.receiver; - -import android.content.BroadcastReceiver; -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.ServiceConnection; -import android.os.IBinder; -import com.deniscerri.ytdlnis.DownloaderService; -import com.deniscerri.ytdlnis.service.IDownloaderListener; -import com.deniscerri.ytdlnis.service.IDownloaderService; - -import java.util.ArrayList; - -public class NotificationReceiver extends BroadcastReceiver { - - public DownloaderService downloaderService; - private ArrayList listeners = null; - private IDownloaderService iDownloaderService; - private Context context; - - private final ServiceConnection serviceConnection = new ServiceConnection() { - @Override - public void onServiceConnected(ComponentName className, IBinder service) { - downloaderService = ((DownloaderService.LocalBinder) service).getService(); - iDownloaderService = (IDownloaderService) service; - cancelDownload(); - } - - @Override - public void onServiceDisconnected(ComponentName componentName) { - downloaderService = null; - iDownloaderService = null; - } - }; - - - @Override - public void onReceive(Context c, Intent intent) { - context = c; - String message = intent.getStringExtra("cancel"); - if (message != null){ - Intent serviceIntent = new Intent(context.getApplicationContext(), DownloaderService.class); - serviceIntent.putExtra("rebind", true); - context.getApplicationContext().bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE); - } - } - - private void cancelDownload(){ - try { - iDownloaderService.cancelDownload(true); - context.getApplicationContext().unbindService(serviceConnection); - context.getApplicationContext().stopService(new Intent(context.getApplicationContext(), DownloaderService.class)); - }catch (Exception ignored){} - } - -} diff --git a/app/src/main/java/com/deniscerri/ytdlnis/receiver/NotificationReceiver.kt b/app/src/main/java/com/deniscerri/ytdlnis/receiver/NotificationReceiver.kt new file mode 100644 index 00000000..d3e1f54c --- /dev/null +++ b/app/src/main/java/com/deniscerri/ytdlnis/receiver/NotificationReceiver.kt @@ -0,0 +1,53 @@ +package com.deniscerri.ytdlnis.receiver + +import android.content.* +import android.os.IBinder +import com.deniscerri.ytdlnis.DownloaderService +import com.deniscerri.ytdlnis.DownloaderService.LocalBinder +import com.deniscerri.ytdlnis.service.IDownloaderService + +class NotificationReceiver : BroadcastReceiver() { + var downloaderService: DownloaderService? = null + private var iDownloaderService: IDownloaderService? = null + private var context: Context? = null + private val serviceConnection: ServiceConnection = object : ServiceConnection { + override fun onServiceConnected(className: ComponentName, service: IBinder) { + downloaderService = (service as LocalBinder).service + iDownloaderService = service + cancelDownload() + } + + override fun onServiceDisconnected(componentName: ComponentName) { + downloaderService = null + iDownloaderService = null + } + } + + override fun onReceive(c: Context, intent: Intent) { + context = c + val message = intent.getStringExtra("cancel") + if (message != null) { + val serviceIntent = Intent(context!!.applicationContext, DownloaderService::class.java) + serviceIntent.putExtra("rebind", true) + context!!.applicationContext.bindService( + serviceIntent, + serviceConnection, + Context.BIND_AUTO_CREATE + ) + } + } + + private fun cancelDownload() { + try { + iDownloaderService!!.cancelDownload(true) + context!!.applicationContext.unbindService(serviceConnection) + context!!.applicationContext.stopService( + Intent( + context!!.applicationContext, + DownloaderService::class.java + ) + ) + } catch (ignored: Exception) { + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/deniscerri/ytdlnis/util/InfoUtil.java b/app/src/main/java/com/deniscerri/ytdlnis/util/InfoUtil.java deleted file mode 100644 index b54a5716..00000000 --- a/app/src/main/java/com/deniscerri/ytdlnis/util/InfoUtil.java +++ /dev/null @@ -1,609 +0,0 @@ -package com.deniscerri.ytdlnis.util; - -import android.app.Activity; -import android.content.Context; -import android.content.SharedPreferences; -import android.text.Html; -import android.util.Log; - -import com.deniscerri.ytdlnis.R; -import com.deniscerri.ytdlnis.database.DatabaseManager; -import com.deniscerri.ytdlnis.database.Video; -import com.yausername.youtubedl_android.YoutubeDL; -import com.yausername.youtubedl_android.YoutubeDLRequest; -import com.yausername.youtubedl_android.YoutubeDLResponse; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.ArrayList; -import java.util.Locale; - -public class InfoUtil { - private static final String TAG = "API MANAGER"; - private static final String invidousURL = "https://invidious.baczek.me/api/v1/"; - private static String countryCODE; - private ArrayList