Check internet connection on MainApp

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-08-12 12:24:33 +02:00
parent 9c21d8ef74
commit e0776d8c37
No known key found for this signature in database
GPG key ID: 4E577DC593B59BDF
2 changed files with 42 additions and 0 deletions

View file

@ -0,0 +1,28 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Alper Ozturk <alper.ozturk@nextcloud.com>
* 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
}
}

View file

@ -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) -> {