Check network connection when file item clicked

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2023-10-25 12:44:49 +02:00
parent 4f23372d67
commit bfccb3bd38
No known key found for this signature in database
GPG key ID: 4E577DC593B59BDF
5 changed files with 69 additions and 33 deletions

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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
}
}
}

View file

@ -44,6 +44,7 @@ import com.google.android.material.snackbar.Snackbar;
import com.nextcloud.client.account.User; import com.nextcloud.client.account.User;
import com.nextcloud.client.account.UserAccountManager; import com.nextcloud.client.account.UserAccountManager;
import com.nextcloud.client.jobs.BackgroundJobManager; import com.nextcloud.client.jobs.BackgroundJobManager;
import com.nextcloud.client.network.ConnectivityObserver;
import com.nextcloud.client.network.ConnectivityService; import com.nextcloud.client.network.ConnectivityService;
import com.nextcloud.utils.EditorUtils; import com.nextcloud.utils.EditorUtils;
import com.owncloud.android.MainApp; 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 @Override
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();

View file

@ -1273,29 +1273,26 @@ public class FileDisplayActivity extends FileActivity
String synchFolderRemotePath = String synchFolderRemotePath =
intent.getStringExtra(FileSyncAdapter.EXTRA_FOLDER_PATH); intent.getStringExtra(FileSyncAdapter.EXTRA_FOLDER_PATH);
RemoteOperationResult synchResult = (RemoteOperationResult) RemoteOperationResult syncResult = (RemoteOperationResult)
DataHolderUtil.getInstance().retrieve(intent.getStringExtra(FileSyncAdapter.EXTRA_RESULT)); DataHolderUtil.getInstance().retrieve(intent.getStringExtra(FileSyncAdapter.EXTRA_RESULT));
boolean sameAccount = getAccount() != null && boolean sameAccount = getAccount() != null &&
accountName.equals(getAccount().name) && getStorageManager() != null; accountName.equals(getAccount().name) && getStorageManager() != null;
if (sameAccount) { if (sameAccount) {
if (FileSyncAdapter.EVENT_FULL_SYNC_START.equals(event)) { if (FileSyncAdapter.EVENT_FULL_SYNC_START.equals(event)) {
mSyncInProgress = true; mSyncInProgress = true;
} else { } else {
OCFile currentFile = (getFile() == null) ? null : OCFile currentFile = (getFile() == null) ? null :
getStorageManager().getFileByPath(getFile().getRemotePath()); getStorageManager().getFileByEncryptedRemotePath(getFile().getRemotePath());
OCFile currentDir = (getCurrentDir() == null) ? null : OCFile currentDir = (getCurrentDir() == null) ? null :
getStorageManager().getFileByPath(getCurrentDir().getRemotePath()); getStorageManager().getFileByEncryptedRemotePath(getCurrentDir().getRemotePath());
if (currentDir == null) { if (currentDir == null) {
// current folder was removed from the server // current folder was removed from the server
DisplayUtils.showSnackMessage( DisplayUtils.showSnackMessage(
getActivity(), getActivity(),
R.string.sync_current_folder_was_removed, R.string.sync_current_folder_was_removed,
synchFolderRemotePath synchFolderRemotePath);
);
browseToRoot(); browseToRoot();
@ -1320,35 +1317,21 @@ public class FileDisplayActivity extends FileActivity
!RefreshFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED.equals(event); !RefreshFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED.equals(event);
if (RefreshFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED.equals(event) && if (RefreshFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED.equals(event) &&
synchResult != null) { syncResult != null) {
if (synchResult.isSuccess()) { if (syncResult.isSuccess()) {
hideInfoBox(); hideInfoBox();
} else { } else {
// TODO refactor and make common // TODO refactor and make common
if (checkForRemoteOperationError(synchResult)) { if (checkForRemoteOperationError(syncResult)) {
requestCredentialsUpdate(context); requestCredentialsUpdate(context);
} else { } else {
switch (synchResult.getCode()) { switch (syncResult.getCode()) {
case SSL_RECOVERABLE_PEER_UNVERIFIED: case SSL_RECOVERABLE_PEER_UNVERIFIED -> showUntrustedCertDialog(syncResult);
showUntrustedCertDialog(synchResult); case MAINTENANCE_MODE -> showInfoBox(R.string.maintenance_mode);
break; case NO_NETWORK_CONNECTION -> showInfoBox(R.string.offline_mode);
case HOST_NOT_AVAILABLE -> showInfoBox(R.string.host_not_available);
case MAINTENANCE_MODE: default -> hideInfoBox();
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;
} }
} }
} }
@ -1374,8 +1357,8 @@ public class FileDisplayActivity extends FileActivity
} }
} }
if (synchResult != null && synchResult.getCode() == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) { if (syncResult != null && syncResult.getCode() == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) {
mLastSslUntrustedServerResult = synchResult; mLastSslUntrustedServerResult = syncResult;
} }
} catch (RuntimeException e) { } catch (RuntimeException e) {
// avoid app crashes after changing the serial id of RemoteOperationResult // avoid app crashes after changing the serial id of RemoteOperationResult

View file

@ -219,7 +219,6 @@ public abstract class ToolbarActivity extends BaseActivity implements Injectable
/** /**
* Hides the toolbar's info box. * Hides the toolbar's info box.
*/ */
@VisibleForTesting
public final void hideInfoBox() { public final void hideInfoBox() {
mInfoBox.setVisibility(View.GONE); mInfoBox.setVisibility(View.GONE);
} }

View file

@ -61,6 +61,7 @@ import com.nextcloud.client.documentscan.DocumentScanActivity;
import com.nextcloud.client.editimage.EditImageActivity; import com.nextcloud.client.editimage.EditImageActivity;
import com.nextcloud.client.jobs.BackgroundJobManager; import com.nextcloud.client.jobs.BackgroundJobManager;
import com.nextcloud.client.network.ClientFactory; import com.nextcloud.client.network.ClientFactory;
import com.nextcloud.client.network.ConnectivityObserver;
import com.nextcloud.client.preferences.AppPreferences; import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.client.utils.Throttler; import com.nextcloud.client.utils.Throttler;
import com.nextcloud.common.NextcloudClient; import com.nextcloud.common.NextcloudClient;
@ -976,6 +977,8 @@ public class OCFileListFragment extends ExtendedListFragment implements
@Override @Override
public void onItemClicked(OCFile file) { public void onItemClicked(OCFile file) {
((FileActivity) mContainerActivity).checkInternetConnection();
if (getCommonAdapter().isMultiSelect()) { if (getCommonAdapter().isMultiSelect()) {
toggleItemToCheckedList(file); toggleItemToCheckedList(file);
} else { } else {