diff --git a/src/androidTest/java/com/owncloud/android/ui/activity/ConflictsResolveActivityIT.java b/src/androidTest/java/com/owncloud/android/ui/activity/ConflictsResolveActivityIT.java index 576932f8ac..188ee46644 100644 --- a/src/androidTest/java/com/owncloud/android/ui/activity/ConflictsResolveActivityIT.java +++ b/src/androidTest/java/com/owncloud/android/ui/activity/ConflictsResolveActivityIT.java @@ -26,7 +26,6 @@ import android.content.Intent; import com.facebook.testing.screenshot.Screenshot; import com.nextcloud.client.account.UserAccountManagerImpl; -import com.nextcloud.java.util.Optional; import com.owncloud.android.AbstractIT; import com.owncloud.android.R; import com.owncloud.android.datamodel.FileDataStorageManager; @@ -84,10 +83,10 @@ public class ConflictsResolveActivityIT extends AbstractIT { ConflictsResolveDialog dialog = new ConflictsResolveDialog(listener, existingFile, newUpload, - Optional.of(UserAccountManagerImpl - .fromContext(targetContext) - .getUser() - )); + UserAccountManagerImpl + .fromContext(targetContext) + .getUser() + ); dialog.showDialog(sut); getInstrumentation().waitForIdleSync(); @@ -136,10 +135,10 @@ public class ConflictsResolveActivityIT extends AbstractIT { ConflictsResolveDialog dialog = new ConflictsResolveDialog(listener, existingFile, newUpload, - Optional.of(UserAccountManagerImpl - .fromContext(targetContext) - .getUser() - )); + UserAccountManagerImpl + .fromContext(targetContext) + .getUser() + ); dialog.showDialog(sut); getInstrumentation().waitForIdleSync(); @@ -177,10 +176,10 @@ public class ConflictsResolveActivityIT extends AbstractIT { ConflictsResolveDialog dialog = new ConflictsResolveDialog(listener, existingFile, newUpload, - Optional.of(UserAccountManagerImpl - .fromContext(targetContext) - .getUser() - )); + UserAccountManagerImpl + .fromContext(targetContext) + .getUser() + ); dialog.showDialog(sut); getInstrumentation().waitForIdleSync(); @@ -218,10 +217,10 @@ public class ConflictsResolveActivityIT extends AbstractIT { ConflictsResolveDialog dialog = new ConflictsResolveDialog(listener, existingFile, newUpload, - Optional.of(UserAccountManagerImpl - .fromContext(targetContext) - .getUser() - )); + UserAccountManagerImpl + .fromContext(targetContext) + .getUser() + ); dialog.showDialog(sut); getInstrumentation().waitForIdleSync(); @@ -263,10 +262,10 @@ public class ConflictsResolveActivityIT extends AbstractIT { ConflictsResolveDialog dialog = new ConflictsResolveDialog(listener, existingFile, newUpload, - Optional.of(UserAccountManagerImpl - .fromContext(targetContext) - .getUser() - )); + UserAccountManagerImpl + .fromContext(targetContext) + .getUser() + ); dialog.showDialog(sut); getInstrumentation().waitForIdleSync(); @@ -308,10 +307,10 @@ public class ConflictsResolveActivityIT extends AbstractIT { ConflictsResolveDialog dialog = new ConflictsResolveDialog(listener, existingFile, newUpload, - Optional.of(UserAccountManagerImpl - .fromContext(targetContext) - .getUser() - )); + UserAccountManagerImpl + .fromContext(targetContext) + .getUser() + ); dialog.showDialog(sut); getInstrumentation().waitForIdleSync(); diff --git a/src/main/java/com/owncloud/android/ui/activity/ConflictsResolveActivity.java b/src/main/java/com/owncloud/android/ui/activity/ConflictsResolveActivity.java index ebb6a8fefe..01b049d72d 100644 --- a/src/main/java/com/owncloud/android/ui/activity/ConflictsResolveActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/ConflictsResolveActivity.java @@ -23,7 +23,10 @@ package com.owncloud.android.ui.activity; import android.content.Intent; import android.os.Bundle; +import android.widget.Toast; +import com.nextcloud.client.account.User; +import com.nextcloud.java.util.Optional; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.datamodel.UploadsStorageManager; import com.owncloud.android.db.OCUpload; @@ -144,33 +147,40 @@ public class ConflictsResolveActivity extends FileActivity implements OnConflict @Override protected void onStart() { super.onStart(); - if (getAccount() != null) { - OCFile file = getFile(); - if (getFile() == null) { - Log_OC.e(TAG, "No file received"); - finish(); - } else { - // Check whether the file is contained in the current Account - Fragment prev = getSupportFragmentManager().findFragmentByTag("conflictDialog"); - - FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); - if (prev == null) { - if (getStorageManager().fileExists(file.getRemotePath())) { - ConflictsResolveDialog dialog = new ConflictsResolveDialog(this, - getFile(), - conflictUpload, - getUser() - ); - dialog.show(fragmentTransaction, "conflictDialog"); - } else { - // Account was changed to a different one - just finish - finish(); - } - } - } - } else { + if (getAccount() == null) { finish(); } + + OCFile file = getFile(); + if (getFile() == null) { + Log_OC.e(TAG, "No file received"); + finish(); + } + + Optional userOptional = getUser(); + + if (!userOptional.isPresent()) { + Toast.makeText(this, "Error creating conflict dialog!", Toast.LENGTH_LONG).show(); + finish(); + } + + // Check whether the file is contained in the current Account + Fragment prev = getSupportFragmentManager().findFragmentByTag("conflictDialog"); + + FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); + if (prev == null) { + if (getStorageManager().fileExists(file.getRemotePath())) { + ConflictsResolveDialog dialog = new ConflictsResolveDialog(this, + getFile(), + conflictUpload, + userOptional.get() + ); + dialog.show(fragmentTransaction, "conflictDialog"); + } else { + // Account was changed to a different one - just finish + finish(); + } + } } /** diff --git a/src/main/java/com/owncloud/android/ui/adapter/LocalFileListAdapter.java b/src/main/java/com/owncloud/android/ui/adapter/LocalFileListAdapter.java index 9f9327d158..14a5cc4350 100644 --- a/src/main/java/com/owncloud/android/ui/adapter/LocalFileListAdapter.java +++ b/src/main/java/com/owncloud/android/ui/adapter/LocalFileListAdapter.java @@ -160,7 +160,7 @@ public class LocalFileListAdapter extends RecyclerView.Adapter asyncTasks, + boolean gridView, + Context context) { if (file.isFolder()) { thumbnailView.setImageDrawable(MimeTypeUtil .getFolderTypeIcon(file.isSharedWithMe() || file.isSharedWithSharee(), file.isSharedViaLink(), file.isEncrypted(), - file.getMountType(), activity)); + file.getMountType(), context)); } else { if (file.getRemoteId() != null && file.isPreviewAvailable()) { // Thumbnail in cache? @@ -615,7 +628,7 @@ public class OCFileListAdapter extends RecyclerView.Adapter asyncTasks = new ArrayList<>(); + private static final String KEY_FILE = "file"; + private static final String KEY_OCFILE = "ocfile"; + private static final String KEY_USER = "user"; + public enum Decision { CANCEL, KEEP_BOTH, @@ -80,11 +80,11 @@ public class ConflictsResolveDialog extends DialogFragment { public ConflictsResolveDialog(OnConflictDecisionMadeListener listener, OCFile file, - OCUpload conflictUpload, Optional user) { + OCUpload conflictUpload, User user) { this.listener = listener; this.existingFile = file; this.newFile = new File(conflictUpload.getLocalPath()); - this.user = user.get(); + this.user = user; } @Override @@ -93,7 +93,7 @@ public class ConflictsResolveDialog extends DialogFragment { AlertDialog alertDialog = (AlertDialog) getDialog(); - if (alertDialog == null || user == null) { + if (alertDialog == null) { Toast.makeText(getContext(), "Failed to create conflict dialog", Toast.LENGTH_LONG).show(); return; } @@ -108,9 +108,9 @@ public class ConflictsResolveDialog extends DialogFragment { super.onCreate(savedInstanceState); if (savedInstanceState != null) { - newFile = (File) savedInstanceState.getSerializable("file"); - existingFile = savedInstanceState.getParcelable("ocfile"); - user = savedInstanceState.getParcelable("user"); + newFile = (File) savedInstanceState.getSerializable(KEY_FILE); + existingFile = savedInstanceState.getParcelable(KEY_OCFILE); + user = savedInstanceState.getParcelable(KEY_USER); listener = (OnConflictDecisionMadeListener) getActivity(); } } @@ -119,9 +119,9 @@ public class ConflictsResolveDialog extends DialogFragment { public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - outState.putSerializable("file", newFile); - outState.putParcelable("ocfile", existingFile); - outState.putParcelable("user", user); + outState.putSerializable(KEY_FILE, newFile); + outState.putParcelable(KEY_OCFILE, existingFile); + outState.putParcelable(KEY_USER, user); } @NonNull @@ -166,7 +166,7 @@ public class ConflictsResolveDialog extends DialogFragment { ImageView newThumbnail = view.findViewById(R.id.new_thumbnail); newThumbnail.setTag(newFile.hashCode()); - setThumbnail(newFile, newThumbnail); + LocalFileListAdapter.setThumbnail(newFile, newThumbnail, getContext()); // set info for existing file TextView existingSize = view.findViewById(R.id.existing_size); @@ -178,7 +178,14 @@ public class ConflictsResolveDialog extends DialogFragment { ImageView existingThumbnail = view.findViewById(R.id.existing_thumbnail); existingThumbnail.setTag(existingFile.getFileId()); - setThumbnail(existingFile, view.findViewById(R.id.existing_thumbnail)); + OCFileListAdapter.setThumbnail(existingFile, + view.findViewById(R.id.existing_thumbnail), + user, + new FileDataStorageManager(user.toPlatformAccount(), + requireContext().getContentResolver()), + asyncTasks, + false, + getContext()); return builder.create(); } @@ -195,7 +202,7 @@ public class ConflictsResolveDialog extends DialogFragment { } @Override - public void onCancel(@NotNull DialogInterface dialog) { + public void onCancel(@NonNull DialogInterface dialog) { if (listener != null) { listener.conflictDecisionMade(Decision.CANCEL); } @@ -205,122 +212,6 @@ public class ConflictsResolveDialog extends DialogFragment { void conflictDecisionMade(Decision decision); } - private void setThumbnail(OCFile file, ImageView thumbnailView) { - if (file.isFolder()) { - thumbnailView.setImageDrawable(MimeTypeUtil - .getFolderTypeIcon(file.isSharedWithMe() || file.isSharedWithSharee(), - file.isSharedViaLink(), - file.isEncrypted(), - file.getMountType(), - getContext())); - } else { - if (file.getRemoteId() != null && file.isPreviewAvailable()) { - // Thumbnail in cache? - Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache( - ThumbnailsCacheManager.PREFIX_THUMBNAIL + file.getRemoteId() - ); - - if (thumbnail != null && !file.isUpdateThumbnailNeeded()) { - if (MimeTypeUtil.isVideo(file)) { - Bitmap withOverlay = ThumbnailsCacheManager.addVideoOverlay(thumbnail); - thumbnailView.setImageBitmap(withOverlay); - } else { - BitmapUtils.setRoundedBitmap(thumbnail, thumbnailView); - } - } else { - // generate new thumbnail - if (ThumbnailsCacheManager.cancelPotentialThumbnailWork(file, thumbnailView)) { - try { - FileDataStorageManager storageManager = - new FileDataStorageManager(user.toPlatformAccount(), - requireContext().getContentResolver()); - final ThumbnailsCacheManager.ThumbnailGenerationTask task = - new ThumbnailsCacheManager.ThumbnailGenerationTask(thumbnailView, - storageManager, - user.toPlatformAccount(), - asyncTasks, - true); - - if (thumbnail == null) { - thumbnail = BitmapUtils.drawableToBitmap( - MimeTypeUtil.getFileTypeIcon(file.getMimeType(), - file.getFileName(), - user.toPlatformAccount(), - getContext())); - } - final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable = - new ThumbnailsCacheManager.AsyncThumbnailDrawable(getResources(), - thumbnail, task); - thumbnailView.setImageDrawable(asyncDrawable); - task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, - file.getRemoteId())); - } catch (IllegalArgumentException e) { - Log_OC.d(this, "ThumbnailGenerationTask : " + e.getMessage()); - } - } - } - - if ("image/png".equalsIgnoreCase(file.getMimeType())) { - thumbnailView.setBackgroundColor(getResources().getColor(R.color.bg_default)); - } - } else { - thumbnailView.setImageDrawable(MimeTypeUtil.getFileTypeIcon(file.getMimeType(), - file.getFileName(), - user.toPlatformAccount(), - getContext())); - } - } - } - - private void setThumbnail(File file, ImageView thumbnailView) { - if (file.isDirectory()) { - thumbnailView.setImageDrawable(MimeTypeUtil.getDefaultFolderIcon(getContext())); - } else { - thumbnailView.setImageResource(R.drawable.file); - - /* Cancellation needs do be checked and done before changing the drawable in fileIcon, or - * {@link ThumbnailsCacheManager#cancelPotentialThumbnailWork} will NEVER cancel any task. - */ - boolean allowedToCreateNewThumbnail = ThumbnailsCacheManager.cancelPotentialThumbnailWork(file, thumbnailView); - - - // get Thumbnail if file is image - if (MimeTypeUtil.isImage(file)) { - // Thumbnail in Cache? - Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache( - ThumbnailsCacheManager.PREFIX_THUMBNAIL + file.hashCode() - ); - if (thumbnail != null) { - thumbnailView.setImageBitmap(thumbnail); - } else { - - // generate new Thumbnail - if (allowedToCreateNewThumbnail) { - final ThumbnailsCacheManager.ThumbnailGenerationTask task = - new ThumbnailsCacheManager.ThumbnailGenerationTask(thumbnailView); - if (MimeTypeUtil.isVideo(file)) { - thumbnail = ThumbnailsCacheManager.mDefaultVideo; - } else { - thumbnail = ThumbnailsCacheManager.mDefaultImg; - } - final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable = - new ThumbnailsCacheManager.AsyncThumbnailDrawable( - getResources(), - thumbnail, - task - ); - thumbnailView.setImageDrawable(asyncDrawable); - task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, null)); - Log_OC.v(this, "Executing task to generate a new thumbnail"); - - } // else, already being generated, don't restart it - } - } else { - thumbnailView.setImageDrawable(MimeTypeUtil.getFileTypeIcon(null, file.getName(), getContext())); - } - } - } - @Override public void onStop() { super.onStop();