cleanup/fix review comments

Signed-off-by: Jens Mueller <tschenser@gmx.de>
This commit is contained in:
Jens Mueller 2019-04-16 19:53:52 +02:00 committed by Andy Scherzinger
parent 7f157a9ff1
commit fe43e503a3
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B

View file

@ -44,6 +44,7 @@ import android.os.ParcelFileDescriptor;
import android.provider.DocumentsContract; import android.provider.DocumentsContract;
import android.provider.DocumentsProvider; import android.provider.DocumentsProvider;
import android.util.Log; import android.util.Log;
import android.util.SparseArray;
import android.widget.Toast; import android.widget.Toast;
import com.nextcloud.client.account.UserAccountManager; import com.nextcloud.client.account.UserAccountManager;
@ -84,9 +85,7 @@ import java.io.IOException;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -106,10 +105,13 @@ public class DocumentsStorageProvider extends DocumentsProvider {
private static final long CACHE_EXPIRATION = TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES); private static final long CACHE_EXPIRATION = TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES);
private static final String ROOT_PATH = "/";
public static final String PATH_SEPARATOR = "/";
UserAccountManager accountManager; UserAccountManager accountManager;
private static final String ROOT_SEPARATOR = "/"; private static final String DOCUMENTID_SEPARATOR = "/";
private final Map<Integer, FileDataStorageManager> rootIdToStorageManager = new HashMap<>(); private final SparseArray<FileDataStorageManager> rootIdToStorageManager = new SparseArray<>();
private final Executor executor = Executors.newCachedThreadPool(); private final Executor executor = Executors.newCachedThreadPool();
@ -118,7 +120,6 @@ public class DocumentsStorageProvider extends DocumentsProvider {
@Override @Override
public Cursor queryRoots(String[] projection) { public Cursor queryRoots(String[] projection) {
Log.d(TAG, "queryRoots()");
Context context = MainApp.getAppContext(); Context context = MainApp.getAppContext();
AppPreferences preferences = AppPreferencesImpl.fromContext(context); AppPreferences preferences = AppPreferencesImpl.fromContext(context);
@ -128,8 +129,8 @@ public class DocumentsStorageProvider extends DocumentsProvider {
} }
final RootCursor result = new RootCursor(projection); final RootCursor result = new RootCursor(projection);
for (Map.Entry<Integer, FileDataStorageManager> entry : rootIdToStorageManager.entrySet()) { for(int i = 0; i < rootIdToStorageManager.size(); i++) {
result.addRoot(new Document(entry.getValue(), "/"), getContext()); result.addRoot(new Document(rootIdToStorageManager.valueAt(i), ROOT_PATH), getContext());
} }
return result; return result;
@ -234,11 +235,10 @@ public class DocumentsStorageProvider extends DocumentsProvider {
OCFile finalFile = ocFile; OCFile finalFile = ocFile;
Thread syncThread = new Thread(() -> { Thread syncThread = new Thread(() -> {
try { try {
FileDataStorageManager sm = FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver());
new FileDataStorageManager(account, context.getContentResolver()); RemoteOperationResult result = new SynchronizeFileOperation(finalFile, null, account,
SynchronizeFileOperation sfo = true, context)
new SynchronizeFileOperation(finalFile, null, account, true, context); .execute(storageManager, context);
RemoteOperationResult result = sfo.execute(sm, context);
if (result.getCode() == RemoteOperationResult.ResultCode.SYNC_CONFLICT) { if (result.getCode() == RemoteOperationResult.ResultCode.SYNC_CONFLICT) {
// ISSUE 5: if the user is not running the app (this is a service!), // ISSUE 5: if the user is not running the app (this is a service!),
// this can be very intrusive; a notification should be preferred // this can be very intrusive; a notification should be preferred
@ -335,7 +335,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
} }
Uri uri = Uri.parse(UriUtils.URI_CONTENT_SCHEME + context.getResources().getString( Uri uri = Uri.parse(UriUtils.URI_CONTENT_SCHEME + context.getResources().getString(
R.string.image_cache_provider_authority) + document.getFile().getRemotePath()); R.string.image_cache_provider_authority) + document.getRemotePath());
Log.d(TAG, "open thumbnail, uri=" + uri); Log.d(TAG, "open thumbnail, uri=" + uri);
return context.getContentResolver().openAssetFileDescriptor(uri, "r"); return context.getContentResolver().openAssetFileDescriptor(uri, "r");
} }
@ -355,7 +355,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
throw new FileNotFoundException("Context may not be null!"); throw new FileNotFoundException("Context may not be null!");
} }
RemoteOperationResult result = new RenameFileOperation(document.getFile().getRemotePath(), displayName) RemoteOperationResult result = new RenameFileOperation(document.getRemotePath(), displayName)
.execute(document.getClient(), document.getStorageManager()); .execute(document.getClient(), document.getStorageManager());
if (!result.isSuccess()) { if (!result.isSuccess()) {
@ -390,7 +390,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
FileDataStorageManager storageManager = document.getStorageManager(); FileDataStorageManager storageManager = document.getStorageManager();
RemoteOperationResult result = new CopyFileOperation(document.getFile().getRemotePath(), targetFolder.getFile().getRemotePath()) RemoteOperationResult result = new CopyFileOperation(document.getRemotePath(), targetFolder.getRemotePath())
.execute(document.getClient(), storageManager); .execute(document.getClient(), storageManager);
if (!result.isSuccess()) { if (!result.isSuccess()) {
@ -402,17 +402,18 @@ public class DocumentsStorageProvider extends DocumentsProvider {
RemoteOperationResult updateParent = new RefreshFolderOperation(targetFolder.getFile(), System.currentTimeMillis(), RemoteOperationResult updateParent = new RefreshFolderOperation(targetFolder.getFile(), System.currentTimeMillis(),
false, false, true, storageManager, false, false, true, storageManager,
account, context).execute(targetFolder.getClient()); account, context)
.execute(targetFolder.getClient());
if (!updateParent.isSuccess()) { if (!updateParent.isSuccess()) {
throw new FileNotFoundException("Failed to copy document with documentId " + sourceDocumentId throw new FileNotFoundException("Failed to copy document with documentId " + sourceDocumentId
+ " to " + targetParentDocumentId); + " to " + targetParentDocumentId);
} }
String newPath = targetFolder.getFile().getRemotePath() + document.getFile().getFileName(); String newPath = targetFolder.getRemotePath() + document.getFile().getFileName();
if (document.getFile().isFolder()) { if (document.getFile().isFolder()) {
newPath = newPath + "/"; newPath = newPath + PATH_SEPARATOR;
} }
Document newFile = new Document(storageManager, newPath); Document newFile = new Document(storageManager, newPath);
@ -447,7 +448,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
throw new FileNotFoundException("Context may not be null!"); throw new FileNotFoundException("Context may not be null!");
} }
RemoteOperationResult result = new MoveFileOperation(document.getFile().getRemotePath(), targetFolder.getFile().getRemotePath()) RemoteOperationResult result = new MoveFileOperation(document.getRemotePath(), targetFolder.getRemotePath())
.execute(document.getClient(), document.getStorageManager()); .execute(document.getClient(), document.getStorageManager());
if (!result.isSuccess()) { if (!result.isSuccess()) {
@ -472,7 +473,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
return result; return result;
} }
for (Document d : findFiles(new Document(storageManager, "/"), query)) { for (Document d : findFiles(new Document(storageManager, ROOT_PATH), query)) {
result.addFile(d); result.addFile(d);
} }
@ -505,9 +506,9 @@ public class DocumentsStorageProvider extends DocumentsProvider {
FileDataStorageManager storageManager = targetFolder.getStorageManager(); FileDataStorageManager storageManager = targetFolder.getStorageManager();
CreateFolderOperation createFolderOperation = new CreateFolderOperation(targetFolder.getFile().getRemotePath() + displayName RemoteOperationResult result = new CreateFolderOperation(targetFolder.getRemotePath() + displayName
+ "/", true); + PATH_SEPARATOR, true)
RemoteOperationResult result = createFolderOperation.execute(targetFolder.getClient(), storageManager); .execute(targetFolder.getClient(), storageManager);
if (!result.isSuccess()) { if (!result.isSuccess()) {
@ -520,13 +521,14 @@ public class DocumentsStorageProvider extends DocumentsProvider {
OwnCloudClient client = targetFolder.getClient(); OwnCloudClient client = targetFolder.getClient();
RemoteOperationResult updateParent = new RefreshFolderOperation(targetFolder.getFile(), System.currentTimeMillis(), RemoteOperationResult updateParent = new RefreshFolderOperation(targetFolder.getFile(), System.currentTimeMillis(),
false, false, true, storageManager, false, false, true, storageManager,
account, context).execute(client); account, context)
.execute(client);
if (!updateParent.isSuccess()) { if (!updateParent.isSuccess()) {
throw new FileNotFoundException("Failed to create document with documentId " + targetFolder.getDocumentId()); throw new FileNotFoundException("Failed to create document with documentId " + targetFolder.getDocumentId());
} }
String newDirPath = targetFolder.getFile().getRemotePath() + displayName + "/"; String newDirPath = targetFolder.getRemotePath() + displayName + PATH_SEPARATOR;
Document newFolder = new Document(storageManager, newDirPath); Document newFolder = new Document(storageManager, newDirPath);
context.getContentResolver().notifyChange(toNotifyUri(targetFolder), null, false); context.getContentResolver().notifyChange(toNotifyUri(targetFolder), null, false);
@ -555,7 +557,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
FileUploader.UploadRequester requester = new FileUploader.UploadRequester(); FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
requester.uploadNewFile(context, account, new String[]{emptyFile.getAbsolutePath()}, requester.uploadNewFile(context, account, new String[]{emptyFile.getAbsolutePath()},
new String[]{targetFolder.getFile().getRemotePath() + displayName}, null, new String[]{targetFolder.getRemotePath() + displayName}, null,
FileUploader.LOCAL_BEHAVIOUR_MOVE, true, UploadFileOperation.CREATED_BY_USER, false, FileUploader.LOCAL_BEHAVIOUR_MOVE, true, UploadFileOperation.CREATED_BY_USER, false,
false); false);
@ -576,7 +578,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
throw new FileNotFoundException("Failed to create document with documentId " + targetFolder.getDocumentId()); throw new FileNotFoundException("Failed to create document with documentId " + targetFolder.getDocumentId());
} }
String newFilePath = targetFolder.getFile().getRemotePath() + displayName; String newFilePath = targetFolder.getRemotePath() + displayName;
Document newFile = new Document(storageManager, newFilePath); Document newFile = new Document(storageManager, newFilePath);
context.getContentResolver().notifyChange(toNotifyUri(targetFolder), null, false); context.getContentResolver().notifyChange(toNotifyUri(targetFolder), null, false);
@ -607,10 +609,9 @@ public class DocumentsStorageProvider extends DocumentsProvider {
Document parentFolder = document.getParent(); Document parentFolder = document.getParent();
RemoveFileOperation removeFileOperation = new RemoveFileOperation(document.getFile().getRemotePath(), false, document.getAccount(), true, RemoteOperationResult result = new RemoveFileOperation(document.getRemotePath(), false,
context); document.getAccount(), true, context)
.execute(document.getClient(), document.getStorageManager());
RemoteOperationResult result = removeFileOperation.execute(document.getClient(), document.getStorageManager());
if (!result.isSuccess()) { if (!result.isSuccess()) {
throw new FileNotFoundException("Failed to delete document with documentId " + documentId); throw new FileNotFoundException("Failed to delete document with documentId " + documentId);
@ -652,16 +653,16 @@ public class DocumentsStorageProvider extends DocumentsProvider {
} }
private FileDataStorageManager getStorageManager(String rootId) { private FileDataStorageManager getStorageManager(String rootId) {
for (FileDataStorageManager data : rootIdToStorageManager.values()) { for(int i = 0; i < rootIdToStorageManager.size(); i++) {
if (data.getAccount().name.equals(rootId)) { FileDataStorageManager storageManager = rootIdToStorageManager.valueAt(i);
return data; if (storageManager.getAccount().name.equals(rootId)) {
return storageManager;
} }
} }
return null; return null;
} }
@SuppressLint("UseSparseArrays")
private void initiateStorageMap() { private void initiateStorageMap() {
rootIdToStorageManager.clear(); rootIdToStorageManager.clear();
@ -704,7 +705,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
} }
private Document toDocument(String documentId) { private Document toDocument(String documentId) {
String[] separated = documentId.split(ROOT_SEPARATOR); String[] separated = documentId.split(DOCUMENTID_SEPARATOR);
if (separated.length != 2) { if (separated.length != 2) {
return null; return null;
} }
@ -784,9 +785,9 @@ public class DocumentsStorageProvider extends DocumentsProvider {
} }
public String getDocumentId() { public String getDocumentId() {
for (Map.Entry<Integer, FileDataStorageManager> entry : rootIdToStorageManager.entrySet()) { for(int i = 0; i < rootIdToStorageManager.size(); i++) {
if (Objects.equals(storageManager, entry.getValue())) { if (Objects.equals(storageManager, rootIdToStorageManager.valueAt(i))) {
return entry.getKey() + ROOT_SEPARATOR + fileId; return rootIdToStorageManager.keyAt(i) + DOCUMENTID_SEPARATOR + fileId;
} }
} }
return null; return null;
@ -804,6 +805,10 @@ public class DocumentsStorageProvider extends DocumentsProvider {
return getStorageManager().getFileById(fileId); return getStorageManager().getFileById(fileId);
} }
public String getRemotePath() {
return getFile().getRemotePath();
}
OwnCloudClient getClient() { OwnCloudClient getClient() {
try { try {
OwnCloudAccount ocAccount = new OwnCloudAccount(getAccount(), MainApp.getAppContext()); OwnCloudAccount ocAccount = new OwnCloudAccount(getAccount(), MainApp.getAppContext());