PlayerServiceConnection.kt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * @author Chris Narkiewicz
  5. * Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.client.media
  21. import android.content.ComponentName
  22. import android.content.Context
  23. import android.content.Intent
  24. import android.content.ServiceConnection
  25. import android.os.IBinder
  26. import android.widget.MediaController
  27. import com.nextcloud.client.account.User
  28. import com.owncloud.android.datamodel.OCFile
  29. @Suppress("TooManyFunctions") // implementing large interface
  30. class PlayerServiceConnection(private val context: Context) : MediaController.MediaPlayerControl {
  31. var isConnected: Boolean = false
  32. private set
  33. private var binder: PlayerService.Binder? = null
  34. fun bind() {
  35. val intent = Intent(context, PlayerService::class.java)
  36. context.bindService(intent, connection, Context.BIND_AUTO_CREATE)
  37. }
  38. fun unbind() {
  39. if (isConnected) {
  40. binder = null
  41. isConnected = false
  42. context.unbindService(connection)
  43. }
  44. }
  45. fun start(user: User, file: OCFile, playImmediately: Boolean, position: Int) {
  46. val i = Intent(context, PlayerService::class.java)
  47. i.putExtra(PlayerService.EXTRA_USER, user)
  48. i.putExtra(PlayerService.EXTRA_FILE, file)
  49. i.putExtra(PlayerService.EXTRA_AUTO_PLAY, playImmediately)
  50. i.putExtra(PlayerService.EXTRA_START_POSITION_MS, position)
  51. i.action = PlayerService.ACTION_PLAY
  52. context.startService(i)
  53. }
  54. fun stop(file: OCFile) {
  55. val i = Intent(context, PlayerService::class.java)
  56. i.putExtra(PlayerService.EXTRA_FILE, file)
  57. i.action = PlayerService.ACTION_STOP_FILE
  58. context.startService(i)
  59. }
  60. fun stop() {
  61. val i = Intent(context, PlayerService::class.java)
  62. i.action = PlayerService.ACTION_STOP
  63. context.startService(i)
  64. }
  65. private val connection = object : ServiceConnection {
  66. override fun onServiceDisconnected(name: ComponentName?) {
  67. isConnected = false
  68. binder = null
  69. }
  70. override fun onServiceConnected(name: ComponentName?, localBinder: IBinder?) {
  71. binder = localBinder as PlayerService.Binder
  72. isConnected = true
  73. }
  74. }
  75. // region Media controller
  76. override fun isPlaying(): Boolean {
  77. return binder?.player?.isPlaying ?: false
  78. }
  79. override fun canSeekForward(): Boolean {
  80. return binder?.player?.canSeekForward() ?: false
  81. }
  82. override fun getDuration(): Int {
  83. return binder?.player?.duration ?: 0
  84. }
  85. override fun pause() {
  86. binder?.player?.pause()
  87. }
  88. override fun getBufferPercentage(): Int {
  89. return binder?.player?.bufferPercentage ?: 0
  90. }
  91. override fun seekTo(pos: Int) {
  92. binder?.player?.seekTo(pos)
  93. }
  94. override fun getCurrentPosition(): Int {
  95. return binder?.player?.currentPosition ?: 0
  96. }
  97. override fun canSeekBackward(): Boolean {
  98. return binder?.player?.canSeekBackward() ?: false
  99. }
  100. override fun start() {
  101. binder?.player?.start()
  102. }
  103. override fun getAudioSessionId(): Int {
  104. return 0
  105. }
  106. override fun canPause(): Boolean {
  107. return binder?.player?.canPause() ?: false
  108. }
  109. // endregion
  110. }