Use jetpack lifecycle

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-06-05 16:41:42 +02:00 committed by Alper Öztürk
parent b2696e087e
commit 85adb503c3
5 changed files with 22 additions and 35 deletions

View file

@ -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"

View file

@ -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
@ -23,10 +24,8 @@ 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
@ -34,7 +33,7 @@ import dagger.android.AndroidInjection
import javax.inject.Inject
import javax.inject.Named
class FileTransferService : Service() {
class FileTransferService : LifecycleService() {
companion object {
const val TAG = "DownloaderService"
@ -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,
@ -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)
}

View file

@ -1,12 +0,0 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Your Name <your@email.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.nextcloud.model
enum class AppLifecycle {
Foreground, Background
}

View file

@ -58,7 +58,6 @@ import com.nextcloud.client.onboarding.OnboardingService;
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;
@ -376,22 +375,10 @@ public class MainApp extends MultiDexApplication implements HasAndroidInjector {
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) {

View file

@ -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>
@ -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"/>
@ -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"/>