|
@@ -2,52 +2,37 @@ package eu.kanade.tachiyomi.data.updater
|
|
|
|
|
|
import android.app.IntentService
|
|
|
import android.app.PendingIntent
|
|
|
-import android.content.BroadcastReceiver
|
|
|
import android.content.Context
|
|
|
import android.content.Intent
|
|
|
-import android.content.IntentFilter
|
|
|
import eu.kanade.tachiyomi.BuildConfig
|
|
|
+import eu.kanade.tachiyomi.R
|
|
|
import eu.kanade.tachiyomi.network.GET
|
|
|
import eu.kanade.tachiyomi.network.NetworkHelper
|
|
|
import eu.kanade.tachiyomi.network.ProgressListener
|
|
|
import eu.kanade.tachiyomi.network.newCallWithProgress
|
|
|
-import eu.kanade.tachiyomi.util.registerLocalReceiver
|
|
|
+import eu.kanade.tachiyomi.util.getUriCompat
|
|
|
import eu.kanade.tachiyomi.util.saveTo
|
|
|
-import eu.kanade.tachiyomi.util.sendLocalBroadcastSync
|
|
|
-import eu.kanade.tachiyomi.util.unregisterLocalReceiver
|
|
|
import timber.log.Timber
|
|
|
import uy.kohesive.injekt.injectLazy
|
|
|
import java.io.File
|
|
|
|
|
|
-class UpdateDownloaderService : IntentService(UpdateDownloaderService::class.java.name) {
|
|
|
+class UpdaterService : IntentService(UpdaterService::class.java.name) {
|
|
|
/**
|
|
|
* Network helper
|
|
|
*/
|
|
|
private val network: NetworkHelper by injectLazy()
|
|
|
|
|
|
/**
|
|
|
- * Local [BroadcastReceiver] that runs on UI thread
|
|
|
+ * Notifier for the updater state and progress.
|
|
|
*/
|
|
|
- private val updaterNotificationReceiver = UpdateDownloaderReceiver(this)
|
|
|
-
|
|
|
-
|
|
|
- override fun onCreate() {
|
|
|
- super.onCreate()
|
|
|
- // Register receiver
|
|
|
- registerLocalReceiver(updaterNotificationReceiver, IntentFilter(INTENT_FILTER_NAME))
|
|
|
- }
|
|
|
-
|
|
|
- override fun onDestroy() {
|
|
|
- // Unregister receiver
|
|
|
- unregisterLocalReceiver(updaterNotificationReceiver)
|
|
|
- super.onDestroy()
|
|
|
- }
|
|
|
+ private val notifier by lazy { UpdaterNotifier(this) }
|
|
|
|
|
|
override fun onHandleIntent(intent: Intent?) {
|
|
|
if (intent == null) return
|
|
|
|
|
|
+ val title = intent.getStringExtra(EXTRA_DOWNLOAD_TITLE) ?: getString(R.string.app_name)
|
|
|
val url = intent.getStringExtra(EXTRA_DOWNLOAD_URL) ?: return
|
|
|
- downloadApk(url)
|
|
|
+ downloadApk(title, url)
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -55,9 +40,9 @@ class UpdateDownloaderService : IntentService(UpdateDownloaderService::class.jav
|
|
|
*
|
|
|
* @param url url location of file
|
|
|
*/
|
|
|
- fun downloadApk(url: String) {
|
|
|
+ private fun downloadApk(title: String, url: String) {
|
|
|
// Show notification download starting.
|
|
|
- sendInitialBroadcast()
|
|
|
+ notifier.onDownloadStarted(title)
|
|
|
|
|
|
val progressListener = object : ProgressListener {
|
|
|
|
|
@@ -73,7 +58,7 @@ class UpdateDownloaderService : IntentService(UpdateDownloaderService::class.jav
|
|
|
if (progress > savedProgress && currentTime - 200 > lastTick) {
|
|
|
savedProgress = progress
|
|
|
lastTick = currentTime
|
|
|
- sendProgressBroadcast(progress)
|
|
|
+ notifier.onProgressChange(progress)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -91,80 +76,32 @@ class UpdateDownloaderService : IntentService(UpdateDownloaderService::class.jav
|
|
|
response.close()
|
|
|
throw Exception("Unsuccessful response")
|
|
|
}
|
|
|
- sendInstallBroadcast(apkFile.absolutePath)
|
|
|
+ notifier.onDownloadFinished(apkFile.getUriCompat(this))
|
|
|
} catch (error: Exception) {
|
|
|
Timber.e(error)
|
|
|
- sendErrorBroadcast(url)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Show notification download starting.
|
|
|
- */
|
|
|
- private fun sendInitialBroadcast() {
|
|
|
- val intent = Intent(INTENT_FILTER_NAME).apply {
|
|
|
- putExtra(UpdateDownloaderReceiver.EXTRA_ACTION, UpdateDownloaderReceiver.NOTIFICATION_UPDATER_INITIAL)
|
|
|
- }
|
|
|
- sendLocalBroadcastSync(intent)
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Show notification progress changed
|
|
|
- *
|
|
|
- * @param progress progress of download
|
|
|
- */
|
|
|
- private fun sendProgressBroadcast(progress: Int) {
|
|
|
- val intent = Intent(INTENT_FILTER_NAME).apply {
|
|
|
- putExtra(UpdateDownloaderReceiver.EXTRA_ACTION, UpdateDownloaderReceiver.NOTIFICATION_UPDATER_PROGRESS)
|
|
|
- putExtra(UpdateDownloaderReceiver.EXTRA_PROGRESS, progress)
|
|
|
- }
|
|
|
- sendLocalBroadcastSync(intent)
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Show install notification.
|
|
|
- *
|
|
|
- * @param path location of file
|
|
|
- */
|
|
|
- private fun sendInstallBroadcast(path: String){
|
|
|
- val intent = Intent(INTENT_FILTER_NAME).apply {
|
|
|
- putExtra(UpdateDownloaderReceiver.EXTRA_ACTION, UpdateDownloaderReceiver.NOTIFICATION_UPDATER_INSTALL)
|
|
|
- putExtra(UpdateDownloaderReceiver.EXTRA_APK_PATH, path)
|
|
|
+ notifier.onDownloadError(url)
|
|
|
}
|
|
|
- sendLocalBroadcastSync(intent)
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Show error notification.
|
|
|
- *
|
|
|
- * @param url url of file
|
|
|
- */
|
|
|
- private fun sendErrorBroadcast(url: String){
|
|
|
- val intent = Intent(INTENT_FILTER_NAME).apply {
|
|
|
- putExtra(UpdateDownloaderReceiver.EXTRA_ACTION, UpdateDownloaderReceiver.NOTIFICATION_UPDATER_ERROR)
|
|
|
- putExtra(UpdateDownloaderReceiver.EXTRA_APK_URL, url)
|
|
|
- }
|
|
|
- sendLocalBroadcastSync(intent)
|
|
|
}
|
|
|
|
|
|
companion object {
|
|
|
/**
|
|
|
- * Name of Local BroadCastReceiver.
|
|
|
+ * Download url.
|
|
|
*/
|
|
|
- private val INTENT_FILTER_NAME = UpdateDownloaderService::class.java.name
|
|
|
+ internal const val EXTRA_DOWNLOAD_URL = "${BuildConfig.APPLICATION_ID}.UpdaterService.DOWNLOAD_URL"
|
|
|
|
|
|
/**
|
|
|
- * Download url.
|
|
|
+ * Download title
|
|
|
*/
|
|
|
- internal const val EXTRA_DOWNLOAD_URL = "${BuildConfig.APPLICATION_ID}.UpdateDownloaderService.DOWNLOAD_URL"
|
|
|
+ internal const val EXTRA_DOWNLOAD_TITLE = "${BuildConfig.APPLICATION_ID}.UpdaterService.DOWNLOAD_TITLE"
|
|
|
|
|
|
/**
|
|
|
* Downloads a new update and let the user install the new version from a notification.
|
|
|
* @param context the application context.
|
|
|
* @param url the url to the new update.
|
|
|
*/
|
|
|
- fun downloadUpdate(context: Context, url: String) {
|
|
|
- val intent = Intent(context, UpdateDownloaderService::class.java).apply {
|
|
|
+ fun downloadUpdate(context: Context, url: String, title: String = context.getString(R.string.app_name)) {
|
|
|
+ val intent = Intent(context, UpdaterService::class.java).apply {
|
|
|
+ putExtra(EXTRA_DOWNLOAD_TITLE, title)
|
|
|
putExtra(EXTRA_DOWNLOAD_URL, url)
|
|
|
}
|
|
|
context.startService(intent)
|
|
@@ -177,7 +114,7 @@ class UpdateDownloaderService : IntentService(UpdateDownloaderService::class.jav
|
|
|
* @return [PendingIntent]
|
|
|
*/
|
|
|
internal fun downloadApkPendingService(context: Context, url: String): PendingIntent {
|
|
|
- val intent = Intent(context, UpdateDownloaderService::class.java).apply {
|
|
|
+ val intent = Intent(context, UpdaterService::class.java).apply {
|
|
|
putExtra(EXTRA_DOWNLOAD_URL, url)
|
|
|
}
|
|
|
return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
|