Skip to content

Commit

Permalink
Use Work Manager, Remove Foreground Service
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Dec 21, 2023
1 parent 251ef24 commit 6b6370e
Show file tree
Hide file tree
Showing 18 changed files with 275 additions and 322 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
*/
package com.nextcloud.client.files.downloader

import android.content.ComponentName
import android.content.Context
import com.nextcloud.client.account.MockUser
import com.nextcloud.client.jobs.BackgroundJobManager
import com.owncloud.android.datamodel.OCFile
import io.mockk.MockKAnnotations
import io.mockk.every
Expand Down Expand Up @@ -54,16 +54,18 @@ class TransferManagerConnectionTest {
lateinit var secondStatusListener: (TransferManager.Status) -> Unit

@MockK
lateinit var binder: FileTransferService.Binder
lateinit var manager: FileTransferWorker.Manager

@MockK
lateinit var backgroundJobManager: BackgroundJobManager

val file get() = OCFile("/path")
val componentName = ComponentName("", FileTransferService::class.java.simpleName)
val user = MockUser()

@Before
fun setUp() {
MockKAnnotations.init(this, relaxed = true)
connection = TransferManagerConnection(context, user)
connection = TransferManagerConnection(backgroundJobManager, user)
}

@Test
Expand All @@ -76,47 +78,47 @@ class TransferManagerConnectionTest {

// WHEN
// service is bound
connection.onServiceConnected(componentName, binder)
connection.onBound()

// THEN
// all listeners are passed to the service
val listeners = mutableListOf<(Transfer) -> Unit>()
verify { binder.registerTransferListener(capture(listeners)) }
verify { manager.registerTransferListener(capture(listeners)) }
assertEquals(listOf(firstDownloadListener, secondDownloadListener), listeners)
}

@Test
fun listeners_are_set_immediately_when_connected() {
// GIVEN
// service is bound
connection.onServiceConnected(componentName, binder)
connection.onBound()

// WHEN
// listeners are added
connection.registerTransferListener(firstDownloadListener)

// THEN
// listener is forwarded to service
verify { binder.registerTransferListener(firstDownloadListener) }
verify { manager.registerTransferListener(firstDownloadListener) }
}

@Test
fun listeners_are_removed_when_unbinding() {
// GIVEN
// service is bound
// service has some listeners
connection.onServiceConnected(componentName, binder)
connection.onBound()
connection.registerTransferListener(firstDownloadListener)
connection.registerTransferListener(secondDownloadListener)

// WHEN
// service unbound
connection.unbind()
connection.onUnbind()

// THEN
// listeners removed from service
verify { binder.removeTransferListener(firstDownloadListener) }
verify { binder.removeTransferListener(secondDownloadListener) }
verify { manager.removeTransferListener(firstDownloadListener) }
verify { manager.removeTransferListener(secondDownloadListener) }
}

@Test
Expand All @@ -136,12 +138,12 @@ class TransferManagerConnectionTest {
connection.enqueue(request2)
val download2 = Transfer(request2.uuid, TransferState.RUNNING, 50, request2.file, request1)

every { binder.getTransfer(request1.uuid) } returns download1
every { binder.getTransfer(request2.uuid) } returns download2
every { manager.getTransfer(request1.uuid) } returns download1
every { manager.getTransfer(request2.uuid) } returns download2

// WHEN
// service is bound
connection.onServiceConnected(componentName, binder)
connection.onBound()

// THEN
// listeners receive current download state for pending downloads
Expand All @@ -160,13 +162,13 @@ class TransferManagerConnectionTest {
// not bound
// has status listeners
val mockStatus: TransferManager.Status = mockk()
every { binder.status } returns mockStatus
every { manager.status } returns mockStatus
connection.registerStatusListener(firstStatusListener)
connection.registerStatusListener(secondStatusListener)

// WHEN
// service is bound
connection.onServiceConnected(componentName, binder)
connection.onBound()

// THEN
// downloader status is delivered
Expand All @@ -182,19 +184,19 @@ class TransferManagerConnectionTest {

// WHEN
// service is bound
connection.onServiceConnected(componentName, binder)
connection.onBound()

// THEN
// downloader status is not requested
verify(exactly = 0) { binder.status }
verify(exactly = 0) { manager.status }
}

@Test
fun not_running_if_not_connected() {
// GIVEN
// downloader is running
// connection not bound
every { binder.isRunning } returns true
every { manager.isRunning } returns true

// THEN
// not running
Expand All @@ -205,8 +207,8 @@ class TransferManagerConnectionTest {
fun is_running_from_binder_if_connected() {
// GIVEN
// service bound
every { binder.isRunning } returns true
connection.onServiceConnected(componentName, binder)
every { manager.isRunning } returns true
connection.onBound()

// WHEN
// is runnign flag accessed
Expand All @@ -215,7 +217,7 @@ class TransferManagerConnectionTest {
// THEN
// call delegated to binder
assertTrue(isRunning)
verify(exactly = 1) { binder.isRunning }
verify(exactly = 1) { manager.isRunning }
}

@Test
Expand All @@ -227,11 +229,11 @@ class TransferManagerConnectionTest {
connection.enqueue(request)
val download = Transfer(request.uuid, TransferState.RUNNING, 50, request.file, request)
connection.registerTransferListener(firstDownloadListener)
every { binder.getTransfer(request.uuid) } returns download
every { manager.getTransfer(request.uuid) } returns download

// WHEN
// service is bound
connection.onServiceConnected(componentName, binder)
connection.onBound()

// THEN
// missed updates not redelivered
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.nextcloud.android.sso.QueryParam
import org.junit.Assert.assertEquals
import org.junit.Test

class InputStreamBinderTest {
class InputStreamManagerTest {
@Test
fun convertMapToNVP() {
val source = mutableMapOf<String, String>()
Expand Down
4 changes: 0 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,6 @@
android:name=".files.services.FileDownloader"
android:foregroundServiceType="dataSync"
android:exported="false" />
<service
android:name="com.nextcloud.client.files.downloader.FileTransferService"
android:foregroundServiceType="dataSync"
android:exported="false" />
<service
android:name=".files.services.FileUploader"
android:foregroundServiceType="dataSync"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.nextcloud.client.documentscan.DocumentScanActivity;
import com.nextcloud.client.editimage.EditImageActivity;
import com.nextcloud.client.etm.EtmActivity;
import com.nextcloud.client.files.downloader.FileTransferService;
import com.nextcloud.client.files.downloader.FileTransferWorker;
import com.nextcloud.client.jobs.NotificationWork;
import com.nextcloud.client.logger.ui.LogsActivity;
import com.nextcloud.client.logger.ui.LogsViewModel;
Expand Down Expand Up @@ -348,7 +348,7 @@ abstract class ComponentsModule {
abstract PlayerService playerService();

@ContributesAndroidInjector
abstract FileTransferService fileDownloaderService();
abstract FileTransferWorker fileDownloaderService();

@ContributesAndroidInjector
abstract FileSyncService fileSyncService();
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/nextcloud/client/etm/EtmViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class EtmViewModel @Inject constructor(
pageClass = EtmFileTransferFragment::class
)
)
val transferManagerConnection = TransferManagerConnection(context, accountManager.user)
val transferManagerConnection = TransferManagerConnection(backgroundJobManager, accountManager.user)

val preferences: Map<String, String> get() {
return defaultPreferences.all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ class EtmFileTransferFragment : EtmBaseFragment() {

override fun onResume() {
super.onResume()
vm.transferManagerConnection.bind()
vm.transferManagerConnection.onBound()
vm.transferManagerConnection.registerStatusListener(this::onDownloaderStatusChanged)
}

override fun onPause() {
super.onPause()
vm.transferManagerConnection.unbind()
vm.transferManagerConnection.onUnbind()
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
Expand Down
Loading

0 comments on commit 6b6370e

Please sign in to comment.