diff --git a/app/src/main/java/com/nextcloud/client/network/ConnectivityObserver.kt b/app/src/main/java/com/nextcloud/client/network/ConnectivityObserver.kt new file mode 100644 index 0000000000..7b2a83b3e1 --- /dev/null +++ b/app/src/main/java/com/nextcloud/client/network/ConnectivityObserver.kt @@ -0,0 +1,44 @@ +/* + * Nextcloud Android client application + * + * @author Alper Ozturk + * Copyright (C) 2023 Alper Ozturk + * Copyright (C) 2023 Nextcloud GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.nextcloud.client.network + +import android.content.Context +import android.net.ConnectivityManager +import android.net.NetworkCapabilities + +object ConnectivityObserver { + + fun isConnected(context: Context): Boolean { + val connectivityManager: ConnectivityManager = + context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + val nw = connectivityManager.activeNetwork + val actNw = connectivityManager.getNetworkCapabilities(nw) ?: return false + + return when { + actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true + actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true + actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true + actNw.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH) -> true + else -> false + } + } +} diff --git a/app/src/main/java/com/owncloud/android/ui/activity/FileActivity.java b/app/src/main/java/com/owncloud/android/ui/activity/FileActivity.java index 87ca463056..ddbbc28fe6 100644 --- a/app/src/main/java/com/owncloud/android/ui/activity/FileActivity.java +++ b/app/src/main/java/com/owncloud/android/ui/activity/FileActivity.java @@ -44,6 +44,7 @@ import com.google.android.material.snackbar.Snackbar; import com.nextcloud.client.account.User; import com.nextcloud.client.account.UserAccountManager; import com.nextcloud.client.jobs.BackgroundJobManager; +import com.nextcloud.client.network.ConnectivityObserver; import com.nextcloud.client.network.ConnectivityService; import com.nextcloud.utils.EditorUtils; import com.owncloud.android.MainApp; @@ -240,6 +241,12 @@ public abstract class FileActivity extends DrawerActivity } } + public void checkInternetConnection() { + if (ConnectivityObserver.INSTANCE.isConnected(this)) { + hideInfoBox(); + } + } + @Override protected void onStart() { super.onStart(); diff --git a/app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java b/app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java index 5c55a80904..b073bd4f84 100644 --- a/app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -1273,29 +1273,26 @@ public class FileDisplayActivity extends FileActivity String synchFolderRemotePath = intent.getStringExtra(FileSyncAdapter.EXTRA_FOLDER_PATH); - RemoteOperationResult synchResult = (RemoteOperationResult) + RemoteOperationResult syncResult = (RemoteOperationResult) DataHolderUtil.getInstance().retrieve(intent.getStringExtra(FileSyncAdapter.EXTRA_RESULT)); boolean sameAccount = getAccount() != null && accountName.equals(getAccount().name) && getStorageManager() != null; if (sameAccount) { - if (FileSyncAdapter.EVENT_FULL_SYNC_START.equals(event)) { mSyncInProgress = true; - } else { OCFile currentFile = (getFile() == null) ? null : - getStorageManager().getFileByPath(getFile().getRemotePath()); + getStorageManager().getFileByEncryptedRemotePath(getFile().getRemotePath()); OCFile currentDir = (getCurrentDir() == null) ? null : - getStorageManager().getFileByPath(getCurrentDir().getRemotePath()); + getStorageManager().getFileByEncryptedRemotePath(getCurrentDir().getRemotePath()); if (currentDir == null) { // current folder was removed from the server DisplayUtils.showSnackMessage( getActivity(), R.string.sync_current_folder_was_removed, - synchFolderRemotePath - ); + synchFolderRemotePath); browseToRoot(); @@ -1320,35 +1317,21 @@ public class FileDisplayActivity extends FileActivity !RefreshFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED.equals(event); if (RefreshFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED.equals(event) && - synchResult != null) { + syncResult != null) { - if (synchResult.isSuccess()) { + if (syncResult.isSuccess()) { hideInfoBox(); } else { // TODO refactor and make common - if (checkForRemoteOperationError(synchResult)) { + if (checkForRemoteOperationError(syncResult)) { requestCredentialsUpdate(context); } else { - switch (synchResult.getCode()) { - case SSL_RECOVERABLE_PEER_UNVERIFIED: - showUntrustedCertDialog(synchResult); - break; - - case MAINTENANCE_MODE: - showInfoBox(R.string.maintenance_mode); - break; - - case NO_NETWORK_CONNECTION: - showInfoBox(R.string.offline_mode); - break; - - case HOST_NOT_AVAILABLE: - showInfoBox(R.string.host_not_available); - break; - - default: - // nothing to do - break; + switch (syncResult.getCode()) { + case SSL_RECOVERABLE_PEER_UNVERIFIED -> showUntrustedCertDialog(syncResult); + case MAINTENANCE_MODE -> showInfoBox(R.string.maintenance_mode); + case NO_NETWORK_CONNECTION -> showInfoBox(R.string.offline_mode); + case HOST_NOT_AVAILABLE -> showInfoBox(R.string.host_not_available); + default -> hideInfoBox(); } } } @@ -1374,8 +1357,8 @@ public class FileDisplayActivity extends FileActivity } } - if (synchResult != null && synchResult.getCode() == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) { - mLastSslUntrustedServerResult = synchResult; + if (syncResult != null && syncResult.getCode() == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) { + mLastSslUntrustedServerResult = syncResult; } } catch (RuntimeException e) { // avoid app crashes after changing the serial id of RemoteOperationResult diff --git a/app/src/main/java/com/owncloud/android/ui/activity/ToolbarActivity.java b/app/src/main/java/com/owncloud/android/ui/activity/ToolbarActivity.java index d65080c118..bc2618a73f 100644 --- a/app/src/main/java/com/owncloud/android/ui/activity/ToolbarActivity.java +++ b/app/src/main/java/com/owncloud/android/ui/activity/ToolbarActivity.java @@ -219,7 +219,6 @@ public abstract class ToolbarActivity extends BaseActivity implements Injectable /** * Hides the toolbar's info box. */ - @VisibleForTesting public final void hideInfoBox() { mInfoBox.setVisibility(View.GONE); } diff --git a/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java b/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java index e7745f3ba3..7431b418eb 100644 --- a/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java +++ b/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java @@ -61,6 +61,7 @@ import com.nextcloud.client.documentscan.DocumentScanActivity; import com.nextcloud.client.editimage.EditImageActivity; import com.nextcloud.client.jobs.BackgroundJobManager; import com.nextcloud.client.network.ClientFactory; +import com.nextcloud.client.network.ConnectivityObserver; import com.nextcloud.client.preferences.AppPreferences; import com.nextcloud.client.utils.Throttler; import com.nextcloud.common.NextcloudClient; @@ -976,6 +977,8 @@ public class OCFileListFragment extends ExtendedListFragment implements @Override public void onItemClicked(OCFile file) { + ((FileActivity) mContainerActivity).checkInternetConnection(); + if (getCommonAdapter().isMultiSelect()) { toggleItemToCheckedList(file); } else {