LibraryUpdateService.kt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. package eu.kanade.tachiyomi.data.library
  2. import android.app.PendingIntent
  3. import android.app.Service
  4. import android.content.BroadcastReceiver
  5. import android.content.Context
  6. import android.content.Intent
  7. import android.os.IBinder
  8. import android.os.PowerManager
  9. import android.support.v4.app.NotificationCompat
  10. import eu.kanade.tachiyomi.Constants
  11. import eu.kanade.tachiyomi.R
  12. import eu.kanade.tachiyomi.data.database.DatabaseHelper
  13. import eu.kanade.tachiyomi.data.database.models.Category
  14. import eu.kanade.tachiyomi.data.database.models.Manga
  15. import eu.kanade.tachiyomi.data.library.LibraryUpdateService.Companion.start
  16. import eu.kanade.tachiyomi.data.preference.PreferencesHelper
  17. import eu.kanade.tachiyomi.data.preference.getOrDefault
  18. import eu.kanade.tachiyomi.data.source.SourceManager
  19. import eu.kanade.tachiyomi.data.source.online.OnlineSource
  20. import eu.kanade.tachiyomi.ui.main.MainActivity
  21. import eu.kanade.tachiyomi.util.AndroidComponentUtil
  22. import eu.kanade.tachiyomi.util.notification
  23. import eu.kanade.tachiyomi.util.notificationManager
  24. import eu.kanade.tachiyomi.util.syncChaptersWithSource
  25. import rx.Observable
  26. import rx.Subscription
  27. import rx.schedulers.Schedulers
  28. import uy.kohesive.injekt.injectLazy
  29. import java.util.*
  30. import java.util.concurrent.atomic.AtomicInteger
  31. /**
  32. * This class will take care of updating the chapters of the manga from the library. It can be
  33. * started calling the [start] method. If it's already running, it won't do anything.
  34. * While the library is updating, a [PowerManager.WakeLock] will be held until the update is
  35. * completed, preventing the device from going to sleep mode. A notification will display the
  36. * progress of the update, and if case of an unexpected error, this service will be silently
  37. * destroyed.
  38. */
  39. class LibraryUpdateService : Service() {
  40. /**
  41. * Database helper.
  42. */
  43. val db: DatabaseHelper by injectLazy()
  44. /**
  45. * Source manager.
  46. */
  47. val sourceManager: SourceManager by injectLazy()
  48. /**
  49. * Preferences.
  50. */
  51. val preferences: PreferencesHelper by injectLazy()
  52. /**
  53. * Wake lock that will be held until the service is destroyed.
  54. */
  55. private lateinit var wakeLock: PowerManager.WakeLock
  56. /**
  57. * Subscription where the update is done.
  58. */
  59. private var subscription: Subscription? = null
  60. /**
  61. * Id of the library update notification.
  62. */
  63. private val notificationId: Int
  64. get() = Constants.NOTIFICATION_LIBRARY_ID
  65. companion object {
  66. /**
  67. * Key for category to update.
  68. */
  69. const val UPDATE_CATEGORY = "category"
  70. /**
  71. * Key for updating the details instead of the chapters.
  72. */
  73. const val UPDATE_DETAILS = "details"
  74. /**
  75. * Returns the status of the service.
  76. *
  77. * @param context the application context.
  78. * @return true if the service is running, false otherwise.
  79. */
  80. fun isRunning(context: Context): Boolean {
  81. return AndroidComponentUtil.isServiceRunning(context, LibraryUpdateService::class.java)
  82. }
  83. /**
  84. * Starts the service. It will be started only if there isn't another instance already
  85. * running.
  86. *
  87. * @param context the application context.
  88. * @param category a specific category to update, or null for global update.
  89. * @param details whether to update the details instead of the list of chapters.
  90. */
  91. fun start(context: Context, category: Category? = null, details: Boolean = false) {
  92. if (!isRunning(context)) {
  93. val intent = Intent(context, LibraryUpdateService::class.java).apply {
  94. putExtra(UPDATE_DETAILS, details)
  95. category?.let { putExtra(UPDATE_CATEGORY, it.id) }
  96. }
  97. context.startService(intent)
  98. }
  99. }
  100. /**
  101. * Stops the service.
  102. *
  103. * @param context the application context.
  104. */
  105. fun stop(context: Context) {
  106. context.stopService(Intent(context, LibraryUpdateService::class.java))
  107. }
  108. }
  109. /**
  110. * Method called when the service is created. It injects dagger dependencies and acquire
  111. * the wake lock.
  112. */
  113. override fun onCreate() {
  114. super.onCreate()
  115. createAndAcquireWakeLock()
  116. }
  117. /**
  118. * Method called when the service is destroyed. It destroys the running subscription, resets
  119. * the alarm and release the wake lock.
  120. */
  121. override fun onDestroy() {
  122. subscription?.unsubscribe()
  123. LibraryUpdateAlarm.startAlarm(this)
  124. destroyWakeLock()
  125. super.onDestroy()
  126. }
  127. /**
  128. * This method needs to be implemented, but it's not used/needed.
  129. */
  130. override fun onBind(intent: Intent): IBinder? {
  131. return null
  132. }
  133. /**
  134. * Method called when the service receives an intent.
  135. *
  136. * @param intent the start intent from.
  137. * @param flags the flags of the command.
  138. * @param startId the start id of this command.
  139. * @return the start value of the command.
  140. */
  141. override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
  142. if (intent == null) return Service.START_NOT_STICKY
  143. // Unsubscribe from any previous subscription if needed.
  144. subscription?.unsubscribe()
  145. // Update favorite manga. Destroy service when completed or in case of an error.
  146. subscription = Observable
  147. .defer {
  148. val mangaList = getMangaToUpdate(intent)
  149. // Update either chapter list or manga details.
  150. if (!intent.getBooleanExtra(UPDATE_DETAILS, false))
  151. updateChapterList(mangaList)
  152. else
  153. updateDetails(mangaList)
  154. }
  155. .subscribeOn(Schedulers.io())
  156. .subscribe({
  157. }, {
  158. showNotification(getString(R.string.notification_update_error), "")
  159. LibraryUpdateTrigger.setupTask(this)
  160. stopSelf(startId)
  161. }, {
  162. LibraryUpdateTrigger.setupTask(this)
  163. stopSelf(startId)
  164. })
  165. return Service.START_REDELIVER_INTENT
  166. }
  167. /**
  168. * Returns the list of manga to be updated.
  169. *
  170. * @param intent the update intent.
  171. * @return a list of manga to update
  172. */
  173. fun getMangaToUpdate(intent: Intent): List<Manga> {
  174. val categoryId = intent.getIntExtra(UPDATE_CATEGORY, -1)
  175. var listToUpdate = if (categoryId != -1)
  176. db.getLibraryMangas().executeAsBlocking().filter { it.category == categoryId }
  177. else {
  178. val categoriesToUpdate = preferences.libraryUpdateCategories().getOrDefault().map { it.toInt() }
  179. if (categoriesToUpdate.isNotEmpty())
  180. db.getLibraryMangas().executeAsBlocking()
  181. .filter { it.category in categoriesToUpdate }
  182. .distinctBy { it.id }
  183. else
  184. db.getFavoriteMangas().executeAsBlocking().distinctBy { it.id }
  185. }
  186. if (preferences.updateOnlyNonCompleted()) {
  187. listToUpdate = listToUpdate.filter { it.status != Manga.COMPLETED }
  188. }
  189. return listToUpdate
  190. }
  191. /**
  192. * Method that updates the given list of manga. It's called in a background thread, so it's safe
  193. * to do heavy operations or network calls here.
  194. * For each manga it calls [updateManga] and updates the notification showing the current
  195. * progress.
  196. *
  197. * @param mangaToUpdate the list to update
  198. * @return an observable delivering the progress of each update.
  199. */
  200. fun updateChapterList(mangaToUpdate: List<Manga>): Observable<Manga> {
  201. // Initialize the variables holding the progress of the updates.
  202. val count = AtomicInteger(0)
  203. val newUpdates = ArrayList<Manga>()
  204. val failedUpdates = ArrayList<Manga>()
  205. val cancelIntent = PendingIntent.getBroadcast(this, 0,
  206. Intent(this, CancelUpdateReceiver::class.java), 0)
  207. // Emit each manga and update it sequentially.
  208. return Observable.from(mangaToUpdate)
  209. // Notify manga that will update.
  210. .doOnNext { showProgressNotification(it, count.andIncrement, mangaToUpdate.size, cancelIntent) }
  211. // Update the chapters of the manga.
  212. .concatMap { manga ->
  213. updateManga(manga)
  214. // If there's any error, return empty update and continue.
  215. .onErrorReturn {
  216. failedUpdates.add(manga)
  217. Pair(0, 0)
  218. }
  219. // Filter out mangas without new chapters (or failed).
  220. .filter { pair -> pair.first > 0 }
  221. // Convert to the manga that contains new chapters.
  222. .map { manga }
  223. }
  224. // Add manga with new chapters to the list.
  225. .doOnNext { newUpdates.add(it) }
  226. // Notify result of the overall update.
  227. .doOnCompleted {
  228. if (newUpdates.isEmpty()) {
  229. cancelNotification()
  230. } else {
  231. showResultNotification(newUpdates, failedUpdates)
  232. }
  233. }
  234. }
  235. /**
  236. * Updates the chapters for the given manga and adds them to the database.
  237. *
  238. * @param manga the manga to update.
  239. * @return a pair of the inserted and removed chapters.
  240. */
  241. fun updateManga(manga: Manga): Observable<Pair<Int, Int>> {
  242. val source = sourceManager.get(manga.source) as? OnlineSource ?: return Observable.empty()
  243. return source.fetchChapterList(manga)
  244. .map { syncChaptersWithSource(db, it, manga, source) }
  245. }
  246. /**
  247. * Method that updates the details of the given list of manga. It's called in a background
  248. * thread, so it's safe to do heavy operations or network calls here.
  249. * For each manga it calls [updateManga] and updates the notification showing the current
  250. * progress.
  251. *
  252. * @param mangaToUpdate the list to update
  253. * @return an observable delivering the progress of each update.
  254. */
  255. fun updateDetails(mangaToUpdate: List<Manga>): Observable<Manga> {
  256. // Initialize the variables holding the progress of the updates.
  257. val count = AtomicInteger(0)
  258. val cancelIntent = PendingIntent.getBroadcast(this, 0,
  259. Intent(this, CancelUpdateReceiver::class.java), 0)
  260. // Emit each manga and update it sequentially.
  261. return Observable.from(mangaToUpdate)
  262. // Notify manga that will update.
  263. .doOnNext { showProgressNotification(it, count.andIncrement, mangaToUpdate.size, cancelIntent) }
  264. // Update the details of the manga.
  265. .concatMap { manga ->
  266. val source = sourceManager.get(manga.source) as? OnlineSource
  267. ?: return@concatMap Observable.empty<Manga>()
  268. source.fetchMangaDetails(manga).doOnNext { networkManga ->
  269. manga.copyFrom(networkManga)
  270. db.insertManga(manga).executeAsBlocking()
  271. }
  272. }
  273. .doOnCompleted {
  274. cancelNotification()
  275. }
  276. }
  277. /**
  278. * Returns the text that will be displayed in the notification when there are new chapters.
  279. *
  280. * @param updates a list of manga that contains new chapters.
  281. * @param failedUpdates a list of manga that failed to update.
  282. * @return the body of the notification to display.
  283. */
  284. private fun getUpdatedMangasBody(updates: List<Manga>, failedUpdates: List<Manga>): String {
  285. return with(StringBuilder()) {
  286. if (updates.isEmpty()) {
  287. append(getString(R.string.notification_no_new_chapters))
  288. append("\n")
  289. } else {
  290. append(getString(R.string.notification_new_chapters))
  291. for (manga in updates) {
  292. append("\n")
  293. append(manga.title)
  294. }
  295. }
  296. if (!failedUpdates.isEmpty()) {
  297. append("\n\n")
  298. append(getString(R.string.notification_manga_update_failed))
  299. for (manga in failedUpdates) {
  300. append("\n")
  301. append(manga.title)
  302. }
  303. }
  304. toString()
  305. }
  306. }
  307. /**
  308. * Creates and acquires a wake lock until the library is updated.
  309. */
  310. private fun createAndAcquireWakeLock() {
  311. wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).newWakeLock(
  312. PowerManager.PARTIAL_WAKE_LOCK, "LibraryUpdateService:WakeLock")
  313. wakeLock.acquire()
  314. }
  315. /**
  316. * Releases the wake lock if it's held.
  317. */
  318. private fun destroyWakeLock() {
  319. if (wakeLock.isHeld) {
  320. wakeLock.release()
  321. }
  322. }
  323. /**
  324. * Shows the notification with the given title and body.
  325. *
  326. * @param title the title of the notification.
  327. * @param body the body of the notification.
  328. */
  329. private fun showNotification(title: String, body: String) {
  330. notificationManager.notify(notificationId, notification() {
  331. setSmallIcon(R.drawable.ic_refresh_white_24dp_img)
  332. setContentTitle(title)
  333. setContentText(body)
  334. })
  335. }
  336. /**
  337. * Shows the notification containing the currently updating manga and the progress.
  338. *
  339. * @param manga the manga that's being updated.
  340. * @param current the current progress.
  341. * @param total the total progress.
  342. */
  343. private fun showProgressNotification(manga: Manga, current: Int, total: Int, cancelIntent: PendingIntent) {
  344. notificationManager.notify(notificationId, notification() {
  345. setSmallIcon(R.drawable.ic_refresh_white_24dp_img)
  346. setContentTitle(manga.title)
  347. setProgress(total, current, false)
  348. setOngoing(true)
  349. addAction(R.drawable.ic_clear_grey_24dp_img, getString(android.R.string.cancel), cancelIntent)
  350. })
  351. }
  352. /**
  353. * Shows the notification containing the result of the update done by the service.
  354. *
  355. * @param updates a list of manga with new updates.
  356. * @param failed a list of manga that failed to update.
  357. */
  358. private fun showResultNotification(updates: List<Manga>, failed: List<Manga>) {
  359. val title = getString(R.string.notification_update_completed)
  360. val body = getUpdatedMangasBody(updates, failed)
  361. notificationManager.notify(notificationId, notification() {
  362. setSmallIcon(R.drawable.ic_refresh_white_24dp_img)
  363. setContentTitle(title)
  364. setStyle(NotificationCompat.BigTextStyle().bigText(body))
  365. setContentIntent(notificationIntent)
  366. setAutoCancel(true)
  367. })
  368. }
  369. /**
  370. * Cancels the notification.
  371. */
  372. private fun cancelNotification() {
  373. notificationManager.cancel(notificationId)
  374. }
  375. /**
  376. * Property that returns an intent to open the main activity.
  377. */
  378. private val notificationIntent: PendingIntent
  379. get() {
  380. val intent = Intent(this, MainActivity::class.java)
  381. intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
  382. return PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
  383. }
  384. /**
  385. * Class that stops updating the library.
  386. */
  387. class CancelUpdateReceiver : BroadcastReceiver() {
  388. /**
  389. * Method called when user wants a library update.
  390. * @param context the application context.
  391. * @param intent the intent received.
  392. */
  393. override fun onReceive(context: Context, intent: Intent) {
  394. LibraryUpdateService.stop(context)
  395. context.notificationManager.cancel(Constants.NOTIFICATION_LIBRARY_ID)
  396. }
  397. }
  398. }