Merge remote-tracking branch 'origin/master' into dev

This commit is contained in:
Tobias Kaminsky 2024-11-12 02:31:57 +01:00
commit 004c91b92f
9 changed files with 147 additions and 28 deletions

View file

@ -7,8 +7,10 @@
*/ */
package com.owncloud.android.services.firebase; package com.owncloud.android.services.firebase;
import android.content.Intent;
import android.text.TextUtils; import android.text.TextUtils;
import com.google.firebase.messaging.Constants.MessageNotificationKeys;
import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage; import com.google.firebase.messaging.RemoteMessage;
import com.nextcloud.client.account.UserAccountManager; import com.nextcloud.client.account.UserAccountManager;
@ -16,6 +18,7 @@ import com.nextcloud.client.jobs.BackgroundJobManager;
import com.nextcloud.client.jobs.NotificationWork; import com.nextcloud.client.jobs.NotificationWork;
import com.nextcloud.client.preferences.AppPreferences; import com.nextcloud.client.preferences.AppPreferences;
import com.owncloud.android.R; import com.owncloud.android.R;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.utils.PushUtils; import com.owncloud.android.utils.PushUtils;
import java.util.Map; import java.util.Map;
@ -30,14 +33,55 @@ public class NCFirebaseMessagingService extends FirebaseMessagingService {
@Inject UserAccountManager accountManager; @Inject UserAccountManager accountManager;
@Inject BackgroundJobManager backgroundJobManager; @Inject BackgroundJobManager backgroundJobManager;
static final String TAG = "NCFirebaseMessagingService";
// Firebase Messaging may apparently use two intent extras to specify a notification message.
//
// See the following fragments in https://github.com/firebase/firebase-android-sdk/blob/releases/m144_1.release/
// firebase-messaging/src/main/java/com/google/firebase/messaging/FirebaseMessagingService.java#L223
// firebase-messaging/src/main/java/com/google/firebase/messaging/NotificationParams.java#L419
// firebase-messaging/src/main/java/com/google/firebase/messaging/Constants.java#L158
//
// The "old" key is not exposed in com.google.firebase.messaging.Constants.MessageNotificationKeys,
// so we need to define it ourselves.
static final String ENABLE_NOTIFICATION_OLD = MessageNotificationKeys.NOTIFICATION_PREFIX_OLD + "e";
static final String ENABLE_NOTIFICATION_NEW = MessageNotificationKeys.ENABLE_NOTIFICATION;
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
AndroidInjection.inject(this); AndroidInjection.inject(this);
} }
@Override
public void handleIntent(Intent intent) {
Log_OC.d(TAG, "handleIntent - extras: " +
ENABLE_NOTIFICATION_NEW + ": " + intent.getExtras().getString(ENABLE_NOTIFICATION_NEW) + ", " +
ENABLE_NOTIFICATION_OLD + ": " + intent.getExtras().getString(ENABLE_NOTIFICATION_OLD));
// When the app is in background and one of the ENABLE_NOTIFICATION or ENABLE_NOTIFICATION_OLD extras is set
// to "1" in the intent sent from the FCM system code to the FirebaseMessagingService in the application,
// the FCM library code that handles the intent DOES NOT invoke the onMessageReceived method.
// It just displays the notification by itself.
//
// In our case the original FCM message contains dummy values "NEW_NOTIFICATION" and we need to get the
// message in onMessageReceived to decrypt it.
//
// So we cheat here a little, by telling the FCM library that the notification flag is not set.
//
// Code below depends on implementation details of the firebase-messaging library (Firebase Android SDK).
// https://github.com/firebase/firebase-android-sdk/tree/master/firebase-messaging
intent.removeExtra(ENABLE_NOTIFICATION_OLD);
intent.removeExtra(ENABLE_NOTIFICATION_NEW);
intent.putExtra(ENABLE_NOTIFICATION_NEW, "0");
super.handleIntent(intent);
}
@Override @Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) { public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
Log_OC.d(TAG, "onMessageReceived");
final Map<String, String> data = remoteMessage.getData(); final Map<String, String> data = remoteMessage.getData();
final String subject = data.get(NotificationWork.KEY_NOTIFICATION_SUBJECT); final String subject = data.get(NotificationWork.KEY_NOTIFICATION_SUBJECT);
final String signature = data.get(NotificationWork.KEY_NOTIFICATION_SIGNATURE); final String signature = data.get(NotificationWork.KEY_NOTIFICATION_SIGNATURE);
@ -48,6 +92,7 @@ public class NCFirebaseMessagingService extends FirebaseMessagingService {
@Override @Override
public void onNewToken(@NonNull String newToken) { public void onNewToken(@NonNull String newToken) {
Log_OC.d(TAG, "onNewToken");
super.onNewToken(newToken); super.onNewToken(newToken);
if (!TextUtils.isEmpty(getResources().getString(R.string.push_server_url))) { if (!TextUtils.isEmpty(getResources().getString(R.string.push_server_url))) {

View file

@ -32,6 +32,7 @@ class InternalTwoWaySyncWork(
private val appPreferences: AppPreferences private val appPreferences: AppPreferences
) : Worker(context, params) { ) : Worker(context, params) {
private var shouldRun = true private var shouldRun = true
private var operation: SynchronizeFolderOperation? = null
override fun doWork(): Result { override fun doWork(): Result {
Log_OC.d(TAG, "Worker started!") Log_OC.d(TAG, "Worker started!")
@ -66,10 +67,10 @@ class InternalTwoWaySyncWork(
} }
Log_OC.d(TAG, "Folder ${folder.remotePath}: started!") Log_OC.d(TAG, "Folder ${folder.remotePath}: started!")
val operation = SynchronizeFolderOperation(context, folder.remotePath, user, fileDataStorageManager) operation = SynchronizeFolderOperation(context, folder.remotePath, user, fileDataStorageManager, true)
.execute(context) val operationResult = operation?.execute(context)
if (operation.isSuccess) { if (operationResult?.isSuccess == true) {
Log_OC.d(TAG, "Folder ${folder.remotePath}: finished!") Log_OC.d(TAG, "Folder ${folder.remotePath}: finished!")
} else { } else {
Log_OC.d(TAG, "Folder ${folder.remotePath} failed!") Log_OC.d(TAG, "Folder ${folder.remotePath} failed!")
@ -77,7 +78,10 @@ class InternalTwoWaySyncWork(
} }
folder.apply { folder.apply {
internalFolderSyncResult = operation.code.toString() operationResult?.let {
internalFolderSyncResult = it.code.toString()
}
internalFolderSyncTimestamp = System.currentTimeMillis() internalFolderSyncTimestamp = System.currentTimeMillis()
} }
@ -96,6 +100,7 @@ class InternalTwoWaySyncWork(
override fun onStopped() { override fun onStopped() {
Log_OC.d(TAG, "OnStopped of worker called!") Log_OC.d(TAG, "OnStopped of worker called!")
operation?.cancel()
shouldRun = false shouldRun = false
super.onStopped() super.onStopped()
} }

View file

@ -77,7 +77,8 @@ class OfflineSyncWork constructor(
user, user,
true, true,
context, context,
storageManager storageManager,
true
) )
synchronizeFileOperation.execute(context) synchronizeFileOperation.execute(context)
} }

View file

@ -46,6 +46,8 @@ public class SynchronizeFileOperation extends SyncOperation {
private boolean mSyncFileContents; private boolean mSyncFileContents;
private Context mContext; private Context mContext;
private boolean mTransferWasRequested; private boolean mTransferWasRequested;
private final boolean syncInBackgroundWorker;
/** /**
* When 'false', uploads to the server are not done; only downloads or conflict detection. This is a temporal * When 'false', uploads to the server are not done; only downloads or conflict detection. This is a temporal
@ -74,7 +76,8 @@ public class SynchronizeFileOperation extends SyncOperation {
User user, User user,
boolean syncFileContents, boolean syncFileContents,
Context context, Context context,
FileDataStorageManager storageManager) { FileDataStorageManager storageManager,
boolean syncInBackgroundWorker) {
super(storageManager); super(storageManager);
mRemotePath = remotePath; mRemotePath = remotePath;
@ -84,6 +87,7 @@ public class SynchronizeFileOperation extends SyncOperation {
mSyncFileContents = syncFileContents; mSyncFileContents = syncFileContents;
mContext = context; mContext = context;
mAllowUploads = true; mAllowUploads = true;
this.syncInBackgroundWorker = syncInBackgroundWorker;
} }
@ -110,7 +114,8 @@ public class SynchronizeFileOperation extends SyncOperation {
User user, User user,
boolean syncFileContents, boolean syncFileContents,
Context context, Context context,
FileDataStorageManager storageManager) { FileDataStorageManager storageManager,
boolean syncInBackgroundWorker) {
super(storageManager); super(storageManager);
mLocalFile = localFile; mLocalFile = localFile;
@ -130,6 +135,7 @@ public class SynchronizeFileOperation extends SyncOperation {
mSyncFileContents = syncFileContents; mSyncFileContents = syncFileContents;
mContext = context; mContext = context;
mAllowUploads = true; mAllowUploads = true;
this.syncInBackgroundWorker = syncInBackgroundWorker;
} }
@ -159,9 +165,9 @@ public class SynchronizeFileOperation extends SyncOperation {
boolean syncFileContents, boolean syncFileContents,
boolean allowUploads, boolean allowUploads,
Context context, Context context,
FileDataStorageManager storageManager) { FileDataStorageManager storageManager,
boolean syncInBackgroundWorker) {
this(localFile, serverFile, user, syncFileContents, context, storageManager); this(localFile, serverFile, user, syncFileContents, context, storageManager, syncInBackgroundWorker);
mAllowUploads = allowUploads; mAllowUploads = allowUploads;
} }
@ -295,13 +301,29 @@ public class SynchronizeFileOperation extends SyncOperation {
} }
private void requestForDownload(OCFile file) { private void requestForDownload(OCFile file) {
if (syncInBackgroundWorker) {
Log_OC.d("InternalTwoWaySyncWork", "download file: " + file.getFileName()); Log_OC.d("InternalTwoWaySyncWork", "download file: " + file.getFileName());
FileDownloadHelper.Companion.instance().downloadFile( try {
mUser, final var operation = new DownloadFileOperation(mUser, file, mContext);
file); var result = operation.execute(getClient());
mTransferWasRequested = true; mTransferWasRequested = true;
String filename = file.getFileName();
if (filename != null) {
if (result.isSuccess()) {
Log_OC.d(TAG, "requestForDownload completed for: " + file.getFileName());
} else {
Log_OC.d(TAG, "requestForDownload failed for: " + file.getFileName());
}
}
} catch (Exception e) {
Log_OC.d(TAG, "Exception caught at requestForDownload" + e);
}
} else {
FileDownloadHelper.Companion.instance().downloadFile(mUser, file);
}
} }
public boolean transferWasRequested() { public boolean transferWasRequested() {

View file

@ -40,7 +40,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Vector; import java.util.Vector;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@ -87,6 +86,8 @@ public class SynchronizeFolderOperation extends SyncOperation {
private final AtomicBoolean mCancellationRequested; private final AtomicBoolean mCancellationRequested;
private final boolean syncInBackgroundWorker;
/** /**
* Creates a new instance of {@link SynchronizeFolderOperation}. * Creates a new instance of {@link SynchronizeFolderOperation}.
* *
@ -97,7 +98,8 @@ public class SynchronizeFolderOperation extends SyncOperation {
public SynchronizeFolderOperation(Context context, public SynchronizeFolderOperation(Context context,
String remotePath, String remotePath,
User user, User user,
FileDataStorageManager storageManager) { FileDataStorageManager storageManager,
boolean syncInBackgroundWorker) {
super(storageManager); super(storageManager);
mRemotePath = remotePath; mRemotePath = remotePath;
@ -107,6 +109,7 @@ public class SynchronizeFolderOperation extends SyncOperation {
mFilesForDirectDownload = new Vector<>(); mFilesForDirectDownload = new Vector<>();
mFilesToSyncContents = new Vector<>(); mFilesToSyncContents = new Vector<>();
mCancellationRequested = new AtomicBoolean(false); mCancellationRequested = new AtomicBoolean(false);
this.syncInBackgroundWorker = syncInBackgroundWorker;
} }
@ -406,7 +409,8 @@ public class SynchronizeFolderOperation extends SyncOperation {
user, user,
true, true,
mContext, mContext,
getStorageManager() getStorageManager(),
syncInBackgroundWorker
); );
mFilesToSyncContents.add(operation); mFilesToSyncContents.add(operation);
} }
@ -427,7 +431,8 @@ public class SynchronizeFolderOperation extends SyncOperation {
user, user,
true, true,
mContext, mContext,
getStorageManager() getStorageManager(),
syncInBackgroundWorker
); );
mFilesToSyncContents.add(operation); mFilesToSyncContents.add(operation);
} }
@ -441,9 +446,41 @@ public class SynchronizeFolderOperation extends SyncOperation {
} }
private void startDirectDownloads() { private void startDirectDownloads() {
if (syncInBackgroundWorker) {
try {
for (OCFile file: mFilesForDirectDownload) {
synchronized (mCancellationRequested) {
if (mCancellationRequested.get()) {
break;
}
}
if (file == null) {
continue;
}
final var operation = new DownloadFileOperation(user, file, mContext);
var result = operation.execute(getClient());
String filename = file.getFileName();
if (filename == null) {
continue;
}
if (result.isSuccess()) {
Log_OC.d(TAG, "startDirectDownloads completed for: " + file.getFileName());
} else {
Log_OC.d(TAG, "startDirectDownloads failed for: " + file.getFileName());
}
}
} catch (Exception e) {
Log_OC.d(TAG, "Exception caught at startDirectDownloads" + e);
}
} else {
final var fileDownloadHelper = FileDownloadHelper.Companion.instance(); final var fileDownloadHelper = FileDownloadHelper.Companion.instance();
mFilesForDirectDownload.forEach(file -> fileDownloadHelper.downloadFile(user, file)); mFilesForDirectDownload.forEach(file -> fileDownloadHelper.downloadFile(user, file));
} }
}
/** /**
* Performs a list of synchronization operations, determining if a download or upload is needed * Performs a list of synchronization operations, determining if a download or upload is needed

View file

@ -698,7 +698,8 @@ public class OperationsService extends Service {
user, user,
syncFileContents, syncFileContents,
getApplicationContext(), getApplicationContext(),
fileDataStorageManager); fileDataStorageManager,
false);
break; break;
case ACTION_SYNC_FOLDER: case ACTION_SYNC_FOLDER:
@ -707,7 +708,8 @@ public class OperationsService extends Service {
this, // TODO remove this dependency from construction time this, // TODO remove this dependency from construction time
remotePath, remotePath,
user, user,
fileDataStorageManager fileDataStorageManager,
false
); );
break; break;

View file

@ -68,6 +68,7 @@ import com.nextcloud.utils.extensions.ActivityExtensionsKt;
import com.nextcloud.utils.extensions.BundleExtensionsKt; import com.nextcloud.utils.extensions.BundleExtensionsKt;
import com.nextcloud.utils.extensions.FileExtensionsKt; import com.nextcloud.utils.extensions.FileExtensionsKt;
import com.nextcloud.utils.extensions.IntentExtensionsKt; import com.nextcloud.utils.extensions.IntentExtensionsKt;
import com.nextcloud.utils.fileNameValidator.FileNameValidator;
import com.nextcloud.utils.view.FastScrollUtils; import com.nextcloud.utils.view.FastScrollUtils;
import com.owncloud.android.MainApp; import com.owncloud.android.MainApp;
import com.owncloud.android.R; import com.owncloud.android.R;
@ -952,6 +953,12 @@ public class FileDisplayActivity extends FileActivity
connectivityService.isNetworkAndServerAvailable(result -> { connectivityService.isNetworkAndServerAvailable(result -> {
if (result) { if (result) {
boolean isValidFolderPath = FileNameValidator.INSTANCE.checkFolderPath(remotePathBase,getCapabilities(),this);
if (!isValidFolderPath) {
DisplayUtils.showSnackMessage(this, R.string.file_name_validator_error_contains_reserved_names_or_invalid_characters);
return;
}
FileUploadHelper.Companion.instance().uploadNewFiles(getUser().orElseThrow(RuntimeException::new), FileUploadHelper.Companion.instance().uploadNewFiles(getUser().orElseThrow(RuntimeException::new),
filePaths, filePaths,
decryptedRemotePaths, decryptedRemotePaths,

View file

@ -35,7 +35,6 @@ import com.nextcloud.client.jobs.upload.FileUploadWorker;
import com.nextcloud.client.preferences.AppPreferences; import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.utils.extensions.ActivityExtensionsKt; import com.nextcloud.utils.extensions.ActivityExtensionsKt;
import com.nextcloud.utils.extensions.FileExtensionsKt; import com.nextcloud.utils.extensions.FileExtensionsKt;
import com.nextcloud.utils.fileNameValidator.FileNameValidator;
import com.owncloud.android.R; import com.owncloud.android.R;
import com.owncloud.android.databinding.UploadFilesLayoutBinding; import com.owncloud.android.databinding.UploadFilesLayoutBinding;
import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.common.utils.Log_OC;
@ -60,7 +59,6 @@ import javax.inject.Inject;
import androidx.activity.OnBackPressedCallback; import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.SearchView; import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat; import androidx.core.view.MenuItemCompat;

View file

@ -225,7 +225,8 @@ public class FileOperationsHelper {
user, user,
true, true,
fileActivity, fileActivity,
storageManager); storageManager,
false);
RemoteOperationResult result = sfo.execute(fileActivity); RemoteOperationResult result = sfo.execute(fileActivity);
if (result.getCode() == RemoteOperationResult.ResultCode.SYNC_CONFLICT) { if (result.getCode() == RemoteOperationResult.ResultCode.SYNC_CONFLICT) {
@ -303,7 +304,8 @@ public class FileOperationsHelper {
user, user,
true, true,
fileActivity, fileActivity,
storageManager); storageManager,
false);
RemoteOperationResult result = sfo.execute(fileActivity); RemoteOperationResult result = sfo.execute(fileActivity);
fileActivity.dismissLoadingDialog(); fileActivity.dismissLoadingDialog();
if (result.getCode() == RemoteOperationResult.ResultCode.SYNC_CONFLICT) { if (result.getCode() == RemoteOperationResult.ResultCode.SYNC_CONFLICT) {