Skip to content

Commit

Permalink
Check network connection when file item clicked
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Oct 25, 2023
1 parent 4f23372 commit bfccb3b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
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;
Expand Down Expand Up @@ -240,6 +241,12 @@ protected void onCreate(Bundle savedInstanceState) {
}
}

public void checkInternetConnection() {
if (ConnectivityObserver.INSTANCE.isConnected(this)) {
hideInfoBox();
}
}

@Override
protected void onStart() {
super.onStart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1273,29 +1273,26 @@ public void onReceive(Context context, Intent intent) {

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();

Expand All @@ -1320,35 +1317,21 @@ public void onReceive(Context context, Intent intent) {
!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();
}
}
}
Expand All @@ -1374,8 +1357,8 @@ public void onReceive(Context context, Intent intent) {
}
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ protected final void showInfoBox(@StringRes int text) {
/**
* Hides the toolbar's info box.
*/
@VisibleForTesting
public final void hideInfoBox() {
mInfoBox.setVisibility(View.GONE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
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;
Expand Down Expand Up @@ -976,6 +977,8 @@ public boolean onLongItemClicked(OCFile file) {

@Override
public void onItemClicked(OCFile file) {
((FileActivity) mContainerActivity).checkInternetConnection();

if (getCommonAdapter().isMultiSelect()) {
toggleItemToCheckedList(file);
} else {
Expand Down

0 comments on commit bfccb3b

Please sign in to comment.