Skip to content

Commit

Permalink
Use jetpack lifecycle
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Jun 5, 2024
1 parent 3deeaee commit 08c2c3a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 35 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ dependencies {
implementation 'androidx.webkit:webkit:1.11.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.exifinterface:exifinterface:1.3.7'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.1"
implementation "androidx.lifecycle:lifecycle-service:2.8.1"
implementation "androidx.work:work-runtime:$workRuntime"
implementation "androidx.work:work-runtime-ktx:$workRuntime"
implementation "androidx.fragment:fragment-ktx:1.7.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
*/
package com.nextcloud.client.jobs.transfer

import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.IBinder
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleService
import com.nextcloud.client.account.User
import com.nextcloud.client.core.AsyncRunner
import com.nextcloud.client.core.LocalBinder
Expand All @@ -23,18 +24,16 @@ import com.nextcloud.client.logger.Logger
import com.nextcloud.client.network.ClientFactory
import com.nextcloud.client.network.ConnectivityService
import com.nextcloud.client.notifications.AppNotificationManager
import com.nextcloud.model.AppLifecycle
import com.nextcloud.utils.ForegroundServiceHelper
import com.nextcloud.utils.extensions.getParcelableArgument
import com.owncloud.android.MainApp
import com.owncloud.android.datamodel.FileDataStorageManager
import com.owncloud.android.datamodel.ForegroundServiceType
import com.owncloud.android.datamodel.UploadsStorageManager
import dagger.android.AndroidInjection
import javax.inject.Inject
import javax.inject.Named

class FileTransferService : Service() {
class FileTransferService : LifecycleService() {

companion object {
const val TAG = "DownloaderService"
Expand Down Expand Up @@ -98,12 +97,12 @@ class FileTransferService : Service() {
AndroidInjection.inject(this)
}

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
if (intent.action != ACTION_TRANSFER) {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent == null || intent.action != ACTION_TRANSFER) {
return START_NOT_STICKY
}

if (!isRunning && MainApp.getAppLifeCycle() == AppLifecycle.Foreground) {
if (!isRunning && lifecycle.currentState == Lifecycle.State.STARTED) {
ForegroundServiceHelper.startService(
this,
AppNotificationManager.TRANSFER_NOTIFICATION_ID,
Expand All @@ -121,8 +120,8 @@ class FileTransferService : Service() {
return START_NOT_STICKY
}

override fun onBind(intent: Intent?): IBinder? {
val user = intent?.getParcelableArgument(EXTRA_USER, User::class.java) ?: return null
override fun onBind(intent: Intent): IBinder? {
val user = intent.getParcelableArgument(EXTRA_USER, User::class.java) ?: return null
return Binder(getTransferManager(user), this)
}

Expand Down
12 changes: 0 additions & 12 deletions app/src/main/java/com/nextcloud/model/AppLifecycle.kt

This file was deleted.

13 changes: 0 additions & 13 deletions app/src/main/java/com/owncloud/android/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.client.preferences.AppPreferencesImpl;
import com.nextcloud.client.preferences.DarkMode;
import com.nextcloud.model.AppLifecycle;
import com.nextcloud.utils.extensions.ContextExtensionsKt;
import com.nmc.android.ui.LauncherActivity;
import com.owncloud.android.authentication.AuthenticatorActivity;
Expand Down Expand Up @@ -376,22 +375,10 @@ public void onCreate() {
registerGlobalPassCodeProtection();
}

private static AppLifecycle lifecycle = AppLifecycle.Foreground;

public static AppLifecycle getAppLifeCycle() {
return lifecycle;
}

private void setAppLifeCycle(AppLifecycle appLifecycle) {
lifecycle = appLifecycle;
}

private final LifecycleEventObserver lifecycleEventObserver = ((lifecycleOwner, event) -> {
if (event == Lifecycle.Event.ON_START) {
setAppLifeCycle(AppLifecycle.Foreground);
Log_OC.d(TAG, "APP IN FOREGROUND");
} else if (event == Lifecycle.Event.ON_STOP) {
setAppLifeCycle(AppLifecycle.Background);
passCodeManager.setCanAskPin(true);
Log_OC.d(TAG, "APP IN BACKGROUND");
} else if (event == Lifecycle.Event.ON_RESUME) {
Expand Down
12 changes: 12 additions & 0 deletions gradle/verification-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@
<trusted-key id="A42FEF087AF29AE864684EAEE6039456D5BBD4F8" group="io.fabric8" name="kubernetes-client-bom" version="5.12.4"/>
<trusted-key id="A5BD02B93E7A40482EB1D66A5F69AD087600B22C" group="org.ow2.asm"/>
<trusted-key id="A5F483CD733A4EBAEA378B2AE88979FB9B30ACF2">
<trusting group="androidx.annotation"/>
<trusting group="androidx.fragment"/>
<trusting group="androidx.lifecycle"/>
<trusting group="androidx.webkit" name="webkit" version="1.11.0"/>
<trusting group="^androidx[.]compose($|([.].*))" regex="true"/>
</trusted-key>
Expand Down Expand Up @@ -1808,6 +1810,11 @@
<sha256 value="9affa24c6160dc8cadaac9422d4f714e5009537d0243afc4bc74b5b7cf0de4ad" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="androidx.lifecycle" name="lifecycle-livedata" version="2.8.1">
<artifact name="lifecycle-livedata-2.8.1.aar">
<sha256 value="1a280b5d6351515907aaa19d3179ec8076095985e979e323a5972bdc5d6b7945" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="androidx.lifecycle" name="lifecycle-livedata-core" version="2.0.0">
<artifact name="lifecycle-livedata-core-2.0.0.aar">
<sha256 value="fde334ec7e22744c0f5bfe7caf1a84c9d717327044400577bdf9bd921ec4f7bc" origin="Generated by Gradle" reason="Artifact is not signed"/>
Expand Down Expand Up @@ -6579,6 +6586,11 @@
<sha256 value="3edce6b711ba368efe16b9b7aacb0214fbd648414cb9b965953a2e7ed89a819a" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.google.errorprone" name="error_prone_annotations" version="2.3.4">
<artifact name="error_prone_annotations-2.3.4.pom">
<sha256 value="1326738a4b4f7ccacf607b866a11fb85193ef60f6a59461187ce7265f9be5bed" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.google.errorprone" name="error_prone_annotations" version="2.5.1">
<artifact name="error_prone_annotations-2.5.1.pom">
<sha256 value="983ad7fe0fa6f6f91e2d20477f5089eccc3f4d8ec6c51d6eb45583970e279d3c" origin="Generated by Gradle"/>
Expand Down

0 comments on commit 08c2c3a

Please sign in to comment.