diff --git a/app/src/main/java/com/nextcloud/receiver/NetworkChangeReceiver.kt b/app/src/main/java/com/nextcloud/receiver/NetworkChangeReceiver.kt new file mode 100644 index 0000000000..418ec99381 --- /dev/null +++ b/app/src/main/java/com/nextcloud/receiver/NetworkChangeReceiver.kt @@ -0,0 +1,28 @@ +/* + * Nextcloud - Android Client + * + * SPDX-FileCopyrightText: 2024 Alper Ozturk + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +package com.nextcloud.receiver + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.net.ConnectivityManager +import android.net.NetworkCapabilities +import com.owncloud.android.MainApp + +class NetworkChangeReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent?) { + MainApp.setIsNetworkAvailable(isNetworkAvailable(context)) + } + + private fun isNetworkAvailable(context: Context): Boolean { + val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + val activeNetwork = connectivityManager.activeNetwork + val networkCapabilities = connectivityManager.getNetworkCapabilities(activeNetwork) + return networkCapabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true + } +} diff --git a/app/src/main/java/com/owncloud/android/MainApp.java b/app/src/main/java/com/owncloud/android/MainApp.java index 9fd3e2cb7a..efbbbb8d0c 100644 --- a/app/src/main/java/com/owncloud/android/MainApp.java +++ b/app/src/main/java/com/owncloud/android/MainApp.java @@ -31,6 +31,7 @@ import android.content.pm.ActivityInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.res.Resources; +import android.net.ConnectivityManager; import android.os.Build; import android.os.Bundle; import android.os.Environment; @@ -59,6 +60,7 @@ 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.receiver.NetworkChangeReceiver; import com.nextcloud.utils.extensions.ContextExtensionsKt; import com.nmc.android.ui.LauncherActivity; import com.owncloud.android.authentication.AuthenticatorActivity; @@ -204,6 +206,8 @@ public class MainApp extends Application implements HasAndroidInjector { private static AppComponent appComponent; + private final NetworkChangeReceiver networkChangeReceiver = new NetworkChangeReceiver(); + /** * Temporary hack */ @@ -227,6 +231,15 @@ public class MainApp extends Application implements HasAndroidInjector { return powerManagementService; } + private void registerNetworkChangeReceiver() { + IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); + registerReceiver(networkChangeReceiver, filter); + } + + public static void setIsNetworkAvailable(boolean isNetworkAvailable) { + // TODO start pending operations if isNetworkAvailable + } + private String getAppProcessName() { String processName = ""; if(Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { @@ -375,6 +388,7 @@ public class MainApp extends Application implements HasAndroidInjector { } registerGlobalPassCodeProtection(); + registerNetworkChangeReceiver(); } private final LifecycleEventObserver lifecycleEventObserver = ((lifecycleOwner, event) -> {