Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

/**
Expand Down Expand Up @@ -150,6 +152,25 @@ public void corruptedUpload() {
uploadsStorageManager.getAllStoredUploads();
}

@Test
public void getById() {
OCUpload upload = createUpload(account);
long id = uploadsStorageManager.storeUpload(upload);

OCUpload newUpload = uploadsStorageManager.getUploadById(id);

assertNotNull(newUpload);
assertEquals(upload.getLocalAction(), newUpload.getLocalAction());
assertEquals(upload.getFolderUnlockToken(), newUpload.getFolderUnlockToken());
}

@Test
public void getByIdNull() {
OCUpload newUpload = uploadsStorageManager.getUploadById(-1);

assertNull(newUpload);
}

private void insertUploads(Account account, int rowsToInsert) {
for (int i = 0; i < rowsToInsert; i++) {
uploadsStorageManager.storeUpload(createUpload(account));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void cancel() {
Intent intent = new Intent(targetContext, ConflictsResolveActivity.class);
intent.putExtra(ConflictsResolveActivity.EXTRA_FILE, newFile);
intent.putExtra(ConflictsResolveActivity.EXTRA_EXISTING_FILE, existingFile);
intent.putExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD, newUpload);
intent.putExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD_ID, newUpload.getUploadId());

ConflictsResolveActivity sut = activityRule.launchActivity(intent);

Expand Down Expand Up @@ -210,7 +210,7 @@ public void keepExisting() {
Intent intent = new Intent(targetContext, ConflictsResolveActivity.class);
intent.putExtra(ConflictsResolveActivity.EXTRA_FILE, newFile);
intent.putExtra(ConflictsResolveActivity.EXTRA_EXISTING_FILE, existingFile);
intent.putExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD, newUpload);
intent.putExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD_ID, newUpload.getUploadId());

ConflictsResolveActivity sut = activityRule.launchActivity(intent);

Expand Down Expand Up @@ -256,7 +256,7 @@ public void keepNew() {
Intent intent = new Intent(targetContext, ConflictsResolveActivity.class);
intent.putExtra(ConflictsResolveActivity.EXTRA_FILE, newFile);
intent.putExtra(ConflictsResolveActivity.EXTRA_EXISTING_FILE, existingFile);
intent.putExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD, newUpload);
intent.putExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD_ID, newUpload.getUploadId());

ConflictsResolveActivity sut = activityRule.launchActivity(intent);

Expand Down Expand Up @@ -301,7 +301,7 @@ public void keepBoth() {
Intent intent = new Intent(targetContext, ConflictsResolveActivity.class);
intent.putExtra(ConflictsResolveActivity.EXTRA_FILE, newFile);
intent.putExtra(ConflictsResolveActivity.EXTRA_EXISTING_FILE, existingFile);
intent.putExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD, newUpload);
intent.putExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD_ID, newUpload.getUploadId());

ConflictsResolveActivity sut = activityRule.launchActivity(intent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,27 @@ public void notifyObserversNow() {
* @param upload Upload instance to remove from persisted storage.
* @return true when the upload was stored and could be removed.
*/
public int removeUpload(OCUpload upload) {
public int removeUpload(@Nullable OCUpload upload) {
if (upload == null) {
return 0;
} else {
return removeUpload(upload.getUploadId());
}
}

/**
* Remove an upload from the uploads list, known its target account and remote path.
*
* @param id to remove from persisted storage.
* @return true when the upload was stored and could be removed.
*/
public int removeUpload(long id) {
int result = getDB().delete(
ProviderTableMeta.CONTENT_URI_UPLOADS,
ProviderTableMeta._ID + "=?",
new String[]{Long.toString(upload.getUploadId())}
);
Log_OC.d(TAG, "delete returns " + result + " for upload " + upload);
ProviderTableMeta.CONTENT_URI_UPLOADS,
ProviderTableMeta._ID + "=?",
new String[]{Long.toString(id)}
);
Log_OC.d(TAG, "delete returns " + result + " for upload with id " + id);
if (result > 0) {
notifyObserversNow();
}
Expand Down Expand Up @@ -287,6 +301,25 @@ public OCUpload[] getAllStoredUploads() {
return getUploads(null, (String[]) null);
}

public @Nullable
OCUpload getUploadById(long id) {
OCUpload result = null;
Cursor cursor = getDB().query(
ProviderTableMeta.CONTENT_URI_UPLOADS,
null,
ProviderTableMeta._ID + "=?",
new String[]{Long.toString(id)},
"_id ASC");

if (cursor != null) {
if (cursor.moveToFirst()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

result = createOCUploadFromCursor(cursor);
}
}
Log_OC.d(TAG, "Retrieve job " + result + " for id " + id);
return result;
}

private OCUpload[] getUploads(@Nullable String selection, @Nullable String... selectionArgs) {
ArrayList<OCUpload> uploads = new ArrayList<>();
final long pageSize = 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.datamodel.UploadsStorageManager;
import com.owncloud.android.db.OCUpload;
import com.owncloud.android.lib.common.OwnCloudAccount;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
Expand All @@ -57,6 +56,7 @@
import com.owncloud.android.lib.resources.files.FileUtils;
import com.owncloud.android.operations.DownloadFileOperation;
import com.owncloud.android.providers.DocumentsStorageProvider;
import com.owncloud.android.ui.activity.ConflictsResolveActivity;
import com.owncloud.android.ui.activity.FileActivity;
import com.owncloud.android.ui.activity.FileDisplayActivity;
import com.owncloud.android.ui.dialog.SendShareDialog;
Expand Down Expand Up @@ -92,7 +92,6 @@ public class FileDownloader extends Service
public static final String EXTRA_DOWNLOAD_RESULT = "RESULT";
public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
public static final String EXTRA_LINKED_TO_PATH = "LINKED_TO";
public static final String EXTRA_CONFLICT_UPLOAD = "CONFLICT_UPLOAD";
public static final String ACCOUNT_NAME = "ACCOUNT_NAME";

private static final int FOREGROUND_SERVICE_ID = 412;
Expand All @@ -116,7 +115,7 @@ public class FileDownloader extends Service

private Notification mNotification;

private OCUpload conflictUpload;
private long conflictUploadId;

@Inject UserAccountManager accountManager;
@Inject UploadsStorageManager uploadsStorageManager;
Expand Down Expand Up @@ -205,7 +204,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
final String behaviour = intent.getStringExtra(OCFileListFragment.DOWNLOAD_BEHAVIOUR);
String activityName = intent.getStringExtra(SendShareDialog.ACTIVITY_NAME);
String packageName = intent.getStringExtra(SendShareDialog.PACKAGE_NAME);
this.conflictUpload = intent.getParcelableExtra(FileDownloader.EXTRA_CONFLICT_UPLOAD);
conflictUploadId = intent.getLongExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD_ID, -1);
AbstractList<String> requestedDownloads = new Vector<String>();
try {
DownloadFileOperation newDownload = new DownloadFileOperation(user.toPlatformAccount(),
Expand Down Expand Up @@ -644,13 +643,13 @@ private void notifyDownloadResult(DownloadFileOperation download,

// Remove success notification
if (downloadResult.isSuccess()) {
if (this.conflictUpload != null) {
uploadsStorageManager.removeUpload(this.conflictUpload);
if (conflictUploadId > 0) {
uploadsStorageManager.removeUpload(conflictUploadId);
}

// Sleep 2 seconds, so show the notification before remove it
NotificationUtils.cancelWithDelay(mNotificationManager,
R.string.downloader_download_succeeded_ticker, 2000);
R.string.downloader_download_succeeded_ticker, 2000);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ private void notifyUploadResult(UploadFileOperation upload, RemoteOperationResul
if (uploadResult.getCode().equals(ResultCode.SYNC_CONFLICT)) {
intent = ConflictsResolveActivity.createIntent(upload.getFile(),
upload.getAccount(),
upload.getOCUploadId(),
Intent.FLAG_ACTIVITY_CLEAR_TOP,
this);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* ownCloud Android client application
*
* @author Bartek Przybylski
Expand Down Expand Up @@ -54,7 +54,7 @@ public class ConflictsResolveActivity extends FileActivity implements OnConflict
/**
* A nullable upload entry that must be removed when and if the conflict is resolved.
*/
public static final String EXTRA_CONFLICT_UPLOAD = "CONFLICT_UPLOAD";
public static final String EXTRA_CONFLICT_UPLOAD_ID = "CONFLICT_UPLOAD_ID";
/**
* Specify the upload local behaviour when there is no CONFLICT_UPLOAD.
*/
Expand All @@ -65,19 +65,24 @@ public class ConflictsResolveActivity extends FileActivity implements OnConflict

@Inject UploadsStorageManager uploadsStorageManager;

private OCUpload conflictUpload;
private long conflictUploadId;
private OCFile existingFile;
private OCFile newFile;
private int localBehaviour = FileUploader.LOCAL_BEHAVIOUR_FORGET;
protected OnConflictDecisionMadeListener listener;

public static Intent createIntent(OCFile file, Account account, Integer flag, Context context) {
public static Intent createIntent(OCFile file,
Account account,
long conflictUploadId,
Integer flag,
Context context) {
Intent intent = new Intent(context, ConflictsResolveActivity.class);
if (flag != null) {
intent.setFlags(intent.getFlags() | flag);
}
intent.putExtra(ConflictsResolveActivity.EXTRA_FILE, file);
intent.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, account);
intent.putExtra(EXTRA_FILE, file);
intent.putExtra(EXTRA_ACCOUNT, account);
intent.putExtra(EXTRA_CONFLICT_UPLOAD_ID, conflictUploadId);

return intent;
}
Expand All @@ -87,82 +92,75 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (savedInstanceState != null) {
conflictUpload = savedInstanceState.getParcelable(EXTRA_CONFLICT_UPLOAD);
conflictUploadId = savedInstanceState.getLong(EXTRA_CONFLICT_UPLOAD_ID);
existingFile = savedInstanceState.getParcelable(EXTRA_EXISTING_FILE);
localBehaviour = savedInstanceState.getInt(EXTRA_LOCAL_BEHAVIOUR);
} else {
conflictUpload = getIntent().getParcelableExtra(EXTRA_CONFLICT_UPLOAD);
conflictUploadId = getIntent().getLongExtra(EXTRA_CONFLICT_UPLOAD_ID, -1);
existingFile = getIntent().getParcelableExtra(EXTRA_EXISTING_FILE);
localBehaviour = getIntent().getIntExtra(EXTRA_LOCAL_BEHAVIOUR, localBehaviour);
}

if (conflictUpload != null) {
localBehaviour = conflictUpload.getLocalAction();
OCUpload upload = uploadsStorageManager.getUploadById(conflictUploadId);

if (upload != null) {
localBehaviour = upload.getLocalAction();
}

// new file was modified locally in file system
newFile = getFile();

listener = new OnConflictDecisionMadeListener() {
@Override
public void conflictDecisionMade(Decision decision) {
OCFile file = newFile; // local file got changed, so either upload it or replace it again by server
// version

switch (decision) {
case CANCEL:
// nothing to do
break;
case KEEP_LOCAL: // Upload
FileUploader.uploadUpdateFile(
getBaseContext(),
getAccount(),
file,
localBehaviour,
FileUploader.NameCollisionPolicy.OVERWRITE
);

if (conflictUpload != null) {
uploadsStorageManager.removeUpload(conflictUpload);
}
break;
case KEEP_BOTH: // Upload
FileUploader.uploadUpdateFile(
getBaseContext(),
getAccount(),
file,
localBehaviour,
FileUploader.NameCollisionPolicy.RENAME
);

if (conflictUpload != null) {
uploadsStorageManager.removeUpload(conflictUpload);
}
break;
case KEEP_SERVER: // Download
if (!shouldDeleteLocal()) {
// Overwrite local file
Intent intent = new Intent(getBaseContext(), FileDownloader.class);
intent.putExtra(FileDownloader.EXTRA_USER, getUser().orElseThrow(RuntimeException::new));
intent.putExtra(FileDownloader.EXTRA_FILE, file);
if (conflictUpload != null) {
intent.putExtra(FileDownloader.EXTRA_CONFLICT_UPLOAD, conflictUpload);
}
startService(intent);
}
break;
}

finish();
listener = decision -> {
OCFile file = newFile; // local file got changed, so either upload it or replace it again by server
// version

switch (decision) {
case CANCEL:
// nothing to do
break;
case KEEP_LOCAL: // Upload
FileUploader.uploadUpdateFile(
getBaseContext(),
getAccount(),
file,
localBehaviour,
FileUploader.NameCollisionPolicy.OVERWRITE
);

uploadsStorageManager.removeUpload(upload);
break;
case KEEP_BOTH: // Upload
FileUploader.uploadUpdateFile(
getBaseContext(),
getAccount(),
file,
localBehaviour,
FileUploader.NameCollisionPolicy.RENAME
);

uploadsStorageManager.removeUpload(upload);
break;
case KEEP_SERVER: // Download
if (!shouldDeleteLocal()) {
// Overwrite local file
Intent intent = new Intent(getBaseContext(), FileDownloader.class);
intent.putExtra(FileDownloader.EXTRA_USER, getUser().orElseThrow(RuntimeException::new));
intent.putExtra(FileDownloader.EXTRA_FILE, file);
intent.putExtra(EXTRA_CONFLICT_UPLOAD_ID, conflictUploadId);
startService(intent);
}
break;
}

finish();
};
}

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);

outState.putParcelable(EXTRA_CONFLICT_UPLOAD, conflictUpload);
outState.putLong(EXTRA_CONFLICT_UPLOAD_ID, conflictUploadId);
outState.putParcelable(EXTRA_EXISTING_FILE, existingFile);
outState.putInt(EXTRA_LOCAL_BEHAVIOUR, localBehaviour);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
OCFile syncedFile = operation.getLocalFile();
if (!result.isSuccess()) {
if (result.getCode() == ResultCode.SYNC_CONFLICT) {
Intent intent = ConflictsResolveActivity.createIntent(syncedFile, getAccount(), null, this);
Intent intent = ConflictsResolveActivity.createIntent(syncedFile, getAccount(), -1, null, this);
startActivity(intent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ private void openConflictActivity(OCFile file, OCUpload upload) {
Context context = MainApp.getAppContext();
Intent intent = ConflictsResolveActivity.createIntent(file,
upload.getAccount(accountManager),
upload.getUploadId(),
Intent.FLAG_ACTIVITY_NEW_TASK,
context);

Expand Down
Loading