From: masensio Date: Thu, 22 Oct 2015 07:54:39 +0000 (+0200) Subject: Get users/group from Server, to fill in 'Share with' list X-Git-Tag: oc-android-1.9^2~30^2~33 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/c49cc06d3de203308ef7726338aff4a93be89cdd?ds=inline Get users/group from Server, to fill in 'Share with' list --- diff --git a/src/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/com/owncloud/android/datamodel/FileDataStorageManager.java index f0877d66..4d07b56e 100644 --- a/src/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -155,12 +155,12 @@ public class FileDataStorageManager { public Vector getFolderImages(OCFile folder/*, boolean onlyOnDevice*/) { - Vector ret = new Vector(); + Vector ret = new Vector(); if (folder != null) { // TODO better implementation, filtering in the access to database instead of here // TODO Enable when "On Device" is recovered ? Vector tmp = getFolderContent(folder/*, onlyOnDevice*/); - OCFile current = null; + OCFile current = null; for (int i=0; i updatedFiles, Collection filesToRemove - ) { - - Log_OC.d(TAG, "Saving folder " + folder.getRemotePath() + " with " + updatedFiles.size() + ) { + + Log_OC.d(TAG, "Saving folder " + folder.getRemotePath() + " with " + updatedFiles.size() + " children and " + filesToRemove.size() + " files to remove"); - ArrayList operations = + ArrayList operations = new ArrayList(updatedFiles.size()); // prepare operations to insert or update files to save in the given folder @@ -280,8 +280,8 @@ public class FileDataStorageManager { ContentValues cv = new ContentValues(); cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp()); cv.put( - ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, - file.getModificationTimestampAtLastSyncForData() + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, + file.getModificationTimestampAtLastSyncForData() ); cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp()); cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength()); @@ -336,8 +336,8 @@ public class FileDataStorageManager { ProviderTableMeta.CONTENT_URI_DIR, file.getFileId() ) ).withSelection(where, whereArgs).build()); - - File localFolder = + + File localFolder = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file)); if (localFolder.exists()) { removeLocalFolder(localFolder); @@ -362,8 +362,8 @@ public class FileDataStorageManager { ContentValues cv = new ContentValues(); cv.put(ProviderTableMeta.FILE_MODIFIED, folder.getModificationTimestamp()); cv.put( - ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, - folder.getModificationTimestampAtLastSyncForData() + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, + folder.getModificationTimestampAtLastSyncForData() ); cv.put(ProviderTableMeta.FILE_CREATION, folder.getCreationTimestamp()); cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, 0); @@ -492,7 +492,7 @@ public class FileDataStorageManager { private boolean removeFolderInDb(OCFile folder) { Uri folder_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, "" + folder.getFileId()); // URI for recursive deletion - String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?" + " AND " + + String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?" + " AND " + ProviderTableMeta.FILE_PATH + "=?"; String [] whereArgs = new String[]{mAccount.name, folder.getRemotePath()}; int deleted = 0; @@ -559,7 +559,7 @@ public class FileDataStorageManager { return success; } - + /** * Updates database and file system for a file or folder that was moved to a different location. * @@ -630,12 +630,12 @@ public class FileDataStorageManager { // update link to downloaded content - but local move is not done here! String targetLocalPath = defaultSavePath + targetPath + child.getStoragePath().substring(lengthOfOldStoragePath); - + cv.put(ProviderTableMeta.FILE_STORAGE_PATH, targetLocalPath); - + originalPathsToTriggerMediaScan.add(child.getStoragePath()); newPathsToTriggerMediaScan.add(targetLocalPath); - + } if (child.getRemotePath().equals(file.getRemotePath())) { cv.put( @@ -751,7 +751,7 @@ public class FileDataStorageManager { return ret; } - + private Vector getFolderContent(long parentId/*, boolean onlyOnDevice*/) { Vector ret = new Vector(); @@ -781,7 +781,7 @@ public class FileDataStorageManager { OCFile child = createFileInstance(c); // TODO Enable when "On Device" is recovered ? // if (child.isFolder() || !onlyOnDevice || onlyOnDevice && child.isDown()){ - ret.add(child); + ret.add(child); // } } while (c.moveToNext()); } @@ -929,8 +929,8 @@ public class FileDataStorageManager { cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate()); cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken()); cv.put( - ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, - share.getSharedWithDisplayName() + ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, + share.getSharedWithDisplayName() ); cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0); cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId()); @@ -1112,7 +1112,29 @@ public class FileDataStorageManager { String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + ProviderTableMeta.FILE_PARENT + "=?"; String [] whereArgs = new String[] { mAccount.name , String.valueOf(folder.getFileId()) }; - + + if (getContentResolver() != null) { + getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs); + + } else { + try { + getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, + whereArgs); + } catch (RemoteException e) { + Log_OC.e(TAG, "Exception in resetShareFlagsInFolder " + e.getMessage()); + } + } + } + + private void resetShareFlagInAFile(String filePath){ + ContentValues cv = new ContentValues(); + cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, false); + cv.put(ProviderTableMeta.FILE_SHARED_VIA_USERS, false); + cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, ""); + String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + + ProviderTableMeta.FILE_PATH+ "=?"; + String [] whereArgs = new String[] { mAccount.name , filePath }; + if (getContentResolver() != null) { getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs); @@ -1146,7 +1168,7 @@ public class FileDataStorageManager { public void saveShares(Collection shares) { cleanShares(); if (shares != null) { - ArrayList operations = + ArrayList operations = new ArrayList(shares.size()); // prepare operations to insert or update files to save in the given folder @@ -1162,8 +1184,8 @@ public class FileDataStorageManager { cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate()); cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken()); cv.put( - ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, - share.getSharedWithDisplayName() + ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, + share.getSharedWithDisplayName() ); cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0); cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId()); @@ -1174,16 +1196,16 @@ public class FileDataStorageManager { // updating an existing file operations.add( ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE). - withValues(cv). - withSelection(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?", - new String[]{String.valueOf(share.getIdRemoteShared())}) - .build()); + withValues(cv). + withSelection(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?", + new String[]{String.valueOf(share.getIdRemoteShared())}) + .build()); } else { // adding a new file operations.add( ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE). - withValues(cv). - build() + withValues(cv). + build() ); } } @@ -1192,7 +1214,7 @@ public class FileDataStorageManager { if (operations.size() > 0) { @SuppressWarnings("unused") ContentProviderResult[] results = null; - Log_OC.d(TAG, "Sending " + operations.size() + + Log_OC.d(TAG, "Sending " + operations.size() + " operations to FileContentProvider"); try { if (getContentResolver() != null) { @@ -1217,7 +1239,7 @@ public class FileDataStorageManager { resetShareFlagsInAllFiles(); if (sharedFiles != null) { - ArrayList operations = + ArrayList operations = new ArrayList(sharedFiles.size()); // prepare operations to insert or update files to save in the given folder @@ -1225,8 +1247,8 @@ public class FileDataStorageManager { ContentValues cv = new ContentValues(); cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp()); cv.put( - ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, - file.getModificationTimestampAtLastSyncForData() + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, + file.getModificationTimestampAtLastSyncForData() ); cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp()); cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength()); @@ -1240,8 +1262,8 @@ public class FileDataStorageManager { cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name); cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties()); cv.put( - ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, - file.getLastSyncDateForData() + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, + file.getLastSyncDateForData() ); cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.isFavorite() ? 1 : 0); cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag()); @@ -1251,8 +1273,8 @@ public class FileDataStorageManager { cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions()); cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId()); cv.put( - ProviderTableMeta.FILE_UPDATE_THUMBNAIL, - file.needsUpdateThumbnail() ? 1 : 0 + ProviderTableMeta.FILE_UPDATE_THUMBNAIL, + file.needsUpdateThumbnail() ? 1 : 0 ); cv.put( ProviderTableMeta.FILE_IS_DOWNLOADING, @@ -1265,17 +1287,17 @@ public class FileDataStorageManager { // updating an existing file operations.add( ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). - withValues(cv). - withSelection(ProviderTableMeta._ID + "=?", - new String[]{String.valueOf(file.getFileId())}) - .build()); + withValues(cv). + withSelection(ProviderTableMeta._ID + "=?", + new String[]{String.valueOf(file.getFileId())}) + .build()); } else { // adding a new file operations.add( ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI). - withValues(cv). - build() + withValues(cv). + build() ); } } @@ -1284,7 +1306,7 @@ public class FileDataStorageManager { if (operations.size() > 0) { @SuppressWarnings("unused") ContentProviderResult[] results = null; - Log_OC.d(TAG, "Sending " + operations.size() + + Log_OC.d(TAG, "Sending " + operations.size() + " operations to FileContentProvider"); try { if (getContentResolver() != null) { @@ -1321,28 +1343,63 @@ public class FileDataStorageManager { } public void saveSharesDB(ArrayList shares) { - saveShares(shares); - - ArrayList sharedFiles = new ArrayList(); + ArrayList operations = new ArrayList(); - for (OCShare share : shares) { - // Get the path - String path = share.getPath(); - if (share.isFolder()) { - path = path + FileUtils.PATH_SEPARATOR; + // Reset flags & Remove shares for this files + String filePath = ""; + for (OCShare share: shares) { + if (filePath != share.getPath()){ + resetShareFlagInAFile(filePath); + operations = prepareRemoveSharesInFile(filePath, operations); + filePath = share.getPath(); } + } + + // Add operations to insert shares + operations = prepareInsertShares(shares, operations); - // Update OCFile with data from share: ShareByLink and publicLink - OCFile file = getFileByPath(path); - if (file != null) { - if (share.getShareType().equals(ShareType.PUBLIC_LINK)) { - file.setShareViaLink(true); - sharedFiles.add(file); + // apply operations in batch + if (operations.size() > 0) { + Log_OC.d(TAG, "Sending " + operations.size() + " operations to FileContentProvider"); + try { + if (getContentResolver() != null) { + getContentResolver().applyBatch(MainApp.getAuthority(), operations); + + } else { + getContentProviderClient().applyBatch(operations); } + + } catch (OperationApplicationException e) { + Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage()); + + } catch (RemoteException e) { + Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage()); } } - updateSharedFiles(sharedFiles); +// // TODO: review if it is needed +// // Update shared files +// ArrayList sharedFiles = new ArrayList(); +// +// for (OCShare share : shares) { +// // Get the path +// String path = share.getPath(); +// if (share.isFolder()) { +// path = path + FileUtils.PATH_SEPARATOR; +// } +// +// // Update OCFile with data from share: ShareByLink, publicLink and +// OCFile file = getFileByPath(path); +// if (file != null) { +// if (share.getShareType().equals(ShareType.PUBLIC_LINK)) { +// file.setShareViaLink(true); +// sharedFiles.add(file); +// } +// } +// } +// +// // TODO: Review +// updateSharedFiles(sharedFiles); } @@ -1365,8 +1422,8 @@ public class FileDataStorageManager { cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate()); cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken()); cv.put( - ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, - share.getSharedWithDisplayName() + ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, + share.getSharedWithDisplayName() ); cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0); cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId()); @@ -1403,6 +1460,48 @@ public class FileDataStorageManager { } + /** + * Prepare operations to insert or update files to save in the given folder + * @param shares List of shares to insert + * @param operations List of operations + * @return + */ + private ArrayList prepareInsertShares( + ArrayList shares, ArrayList operations) { + + if (shares != null) { + // prepare operations to insert or update files to save in the given folder + for (OCShare share : shares) { + ContentValues cv = new ContentValues(); + cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource()); + cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource()); + cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue()); + cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith()); + cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath()); + cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions()); + cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate()); + cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate()); + cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken()); + cv.put( + ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, + share.getSharedWithDisplayName() + ); + cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0); + cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId()); + cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getIdRemoteShared()); + cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name); + + // adding a new share resource + operations.add( + ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE). + withValues(cv). + build() + ); + } + } + return operations; + } + private ArrayList prepareRemoveSharesInFolder( OCFile folder, ArrayList preparedOperations) { if (folder != null) { @@ -1412,17 +1511,35 @@ public class FileDataStorageManager { // TODO Enable when "On Device" is recovered ? Vector files = getFolderContent(folder /*, false*/); - + for (OCFile file : files) { whereArgs[0] = file.getRemotePath(); preparedOperations.add( ContentProviderOperation.newDelete(ProviderTableMeta.CONTENT_URI_SHARE). - withSelection(where, whereArgs). - build() + withSelection(where, whereArgs). + build() ); } } return preparedOperations; + + } + + private ArrayList prepareRemoveSharesInFile( + String filePath, ArrayList preparedOperations) { + + String where = ProviderTableMeta.OCSHARES_PATH + "=?" + " AND " + + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?"; + String[] whereArgs = new String[]{filePath, mAccount.name}; + + preparedOperations.add( + ContentProviderOperation.newDelete(ProviderTableMeta.CONTENT_URI_SHARE). + withSelection(where, whereArgs). + build() + ); + + return preparedOperations; + } public ArrayList getSharesWithForAFile(String filePath, String accountName){ diff --git a/src/com/owncloud/android/files/FileOperationsHelper.java b/src/com/owncloud/android/files/FileOperationsHelper.java index 495d7a31..d249c020 100644 --- a/src/com/owncloud/android/files/FileOperationsHelper.java +++ b/src/com/owncloud/android/files/FileOperationsHelper.java @@ -259,6 +259,18 @@ public class FileOperationsHelper { } + + /** + * @return 'True' if the server supports the Search Users API + */ + public boolean isSearchUsersSupportedSupported() { + if (mFileActivity.getAccount() != null) { + OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount()); + return (serverVersion != null && serverVersion.isSearchUsersSupported()); + } + return false; + } + public void sendDownloadedFile(OCFile file) { if (file != null) { String storagePath = file.getStoragePath(); diff --git a/src/com/owncloud/android/operations/CreateShareOperation.java b/src/com/owncloud/android/operations/CreateShareOperation.java index ebae9bd8..ade575ce 100644 --- a/src/com/owncloud/android/operations/CreateShareOperation.java +++ b/src/com/owncloud/android/operations/CreateShareOperation.java @@ -31,14 +31,14 @@ import com.owncloud.android.R; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.resources.shares.OCShare; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.utils.Log_OC; +import com.owncloud.android.lib.resources.files.FileUtils; +import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation; import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation; +import com.owncloud.android.lib.resources.shares.OCShare; import com.owncloud.android.lib.resources.shares.ShareType; -import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation; -import com.owncloud.android.lib.resources.files.FileUtils; import com.owncloud.android.operations.common.SyncOperation; public class CreateShareOperation extends SyncOperation { diff --git a/src/com/owncloud/android/operations/GetSharesForFileOperation.java b/src/com/owncloud/android/operations/GetSharesForFileOperation.java index bf136ad3..62c3be18 100644 --- a/src/com/owncloud/android/operations/GetSharesForFileOperation.java +++ b/src/com/owncloud/android/operations/GetSharesForFileOperation.java @@ -21,16 +21,15 @@ package com.owncloud.android.operations; -import java.util.ArrayList; - -import com.owncloud.android.MainApp; import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.resources.shares.OCShare; import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation; +import com.owncloud.android.lib.resources.shares.OCShare; import com.owncloud.android.operations.common.SyncOperation; +import java.util.ArrayList; + /** * Provide a list shares for a specific file. */ diff --git a/src/com/owncloud/android/ui/activity/ShareActivity.java b/src/com/owncloud/android/ui/activity/ShareActivity.java index 8cbc04e4..03d3b833 100644 --- a/src/com/owncloud/android/ui/activity/ShareActivity.java +++ b/src/com/owncloud/android/ui/activity/ShareActivity.java @@ -34,6 +34,7 @@ import com.owncloud.android.providers.UsersAndGroupsSearchProvider; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; +import com.owncloud.android.operations.GetSharesForFileOperation; import com.owncloud.android.operations.UnshareOperation; import com.owncloud.android.ui.fragment.SearchFragment; import com.owncloud.android.ui.fragment.ShareFileFragment; @@ -59,6 +60,7 @@ public class ShareActivity extends FileActivity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + setContentView(R.layout.share_activity); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); @@ -145,6 +147,7 @@ public class ShareActivity extends FileActivity } + @Override public void showSearchUsersAndGroups() { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); @@ -176,15 +179,25 @@ public class ShareActivity extends FileActivity super.onRemoteOperationFinish(operation, result); if (operation instanceof UnshareOperation) { if (mShareFileFragment != null){ - mShareFileFragment.refreshUsersOrGroupsList(); + mShareFileFragment.refreshUsersOrGroupsListFromDB(); } + } else if (operation instanceof GetSharesForFileOperation) { + onGetSharesForFileOperationFinish((GetSharesForFileOperation) operation, result); } } - @Override - public void onShareFragmentInteraction(Uri uri) { + private void onGetSharesForFileOperationFinish(GetSharesForFileOperation operation, RemoteOperationResult result){ + dismissLoadingDialog(); + if (!result.isSuccess()) { + Toast.makeText(getApplicationContext(), result.getLogMessage(), Toast.LENGTH_LONG).show(); + } + + // Show Shares + if (mShareFileFragment != null){ + mShareFileFragment.refreshUsersOrGroupsListFromDB(); + } } @Override diff --git a/src/com/owncloud/android/ui/fragment/ShareFileFragment.java b/src/com/owncloud/android/ui/fragment/ShareFileFragment.java index 38deb99f..8a589db9 100644 --- a/src/com/owncloud/android/ui/fragment/ShareFileFragment.java +++ b/src/com/owncloud/android/ui/fragment/ShareFileFragment.java @@ -23,7 +23,6 @@ package com.owncloud.android.ui.fragment; import android.accounts.Account; import android.app.Activity; import android.graphics.Bitmap; -import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; @@ -34,16 +33,21 @@ import android.widget.Button; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; +import android.widget.Toast; import com.owncloud.android.R; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.datamodel.ThumbnailsCacheManager; +import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.resources.shares.OCShare; +import com.owncloud.android.lib.resources.shares.ShareType; import com.owncloud.android.ui.activity.FileActivity; +import com.owncloud.android.ui.activity.ShareActivity; import com.owncloud.android.ui.adapter.ShareUserListAdapter; import com.owncloud.android.utils.DisplayUtils; +import com.owncloud.android.utils.GetShareWithUsersAsyncTask; import com.owncloud.android.utils.MimetypeIconUtil; import java.util.ArrayList; @@ -58,7 +62,8 @@ import java.util.ArrayList; * Use the {@link ShareFileFragment#newInstance} factory method to * create an instance of this fragment. */ -public class ShareFileFragment extends Fragment { +public class ShareFileFragment extends Fragment + implements GetShareWithUsersAsyncTask.OnGetSharesWithUsersTaskListener { private static final String TAG = ShareFileFragment.class.getSimpleName(); @@ -151,14 +156,9 @@ public class ShareFileFragment extends Fragment { public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - refreshUsersOrGroupsList(); - } + // Load data to the list (start process with an Async Task) + refreshUsersOrGroupsListFromServer(); - // TODO: Rename method, update argument and hook method into UI event - public void onButtonPressed(Uri uri) { - if (mListener != null) { - mListener.onShareFragmentInteraction(uri); - } } @Override @@ -178,10 +178,10 @@ public class ShareFileFragment extends Fragment { mListener = null; } - // Get users and groups to fill the "share with" list - public void refreshUsersOrGroupsList(){ - mShares = new ArrayList<>(); - + /** + * Get users and groups fromn the DB to fill in the "share with" list + */ + public void refreshUsersOrGroupsListFromDB (){ // Get Users and Groups FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(mAccount, getActivity().getContentResolver()); @@ -215,6 +215,21 @@ public class ShareFileFragment extends Fragment { } } + /** + * Get users and groups from the server to fill in the "share with" list + */ + public void refreshUsersOrGroupsListFromServer(){ + mShares = new ArrayList<>(); + + // Show loading + ((ShareActivity) getActivity()).showLoadingDialog(getString(R.string.common_loading)); + // Get Users and Groups + GetShareWithUsersAsyncTask getTask = new GetShareWithUsersAsyncTask(this); + FileDataStorageManager fileDataStorageManager = ((ShareActivity) getActivity()).getStorageManager(); + Object[] params = { mFile, mAccount, fileDataStorageManager}; + getTask.execute(params); + } + private void registerLongClickListener(final ListView listView) { listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override @@ -254,6 +269,41 @@ public class ShareFileFragment extends Fragment { ); } + @Override + public void onGetDataShareWithFinish(RemoteOperationResult result) { + // Remove loading + ((ShareActivity) getActivity()).dismissLoadingDialog(); + if (result != null && result.isSuccess()) { + // update local database + for(Object obj: result.getData()) { + if ( ((OCShare) obj).getShareType() == ShareType.USER || + ((OCShare) obj).getShareType() == ShareType.GROUP ){ + mShares.add((OCShare) obj); + } + } + + // Update list of users/groups + mUserGroupsAdapter = new ShareUserListAdapter(getActivity().getApplicationContext(), + R.layout.share_user_item, mShares); + + // Show data + TextView noShares = (TextView) getView().findViewById(R.id.shareNoUsers); + ListView usersList = (ListView) getView().findViewById(R.id.shareUsersList); + + if (mShares.size() > 0) { + noShares.setVisibility(View.GONE); + usersList.setVisibility(View.VISIBLE); + usersList.setAdapter(mUserGroupsAdapter); + + } else { + noShares.setVisibility(View.VISIBLE); + usersList.setVisibility(View.GONE); + } + } else { + Toast.makeText(getActivity(), result.getLogMessage(), Toast.LENGTH_SHORT).show(); + } + } + // TODO: review if it is necessary /** @@ -268,8 +318,6 @@ public class ShareFileFragment extends Fragment { */ public interface OnShareFragmentInteractionListener { void showSearchUsersAndGroups(); - - void onShareFragmentInteraction(Uri uri); } } diff --git a/src/com/owncloud/android/utils/GetShareWithUsersAsyncTask.java b/src/com/owncloud/android/utils/GetShareWithUsersAsyncTask.java new file mode 100644 index 00000000..23527279 --- /dev/null +++ b/src/com/owncloud/android/utils/GetShareWithUsersAsyncTask.java @@ -0,0 +1,108 @@ +/** + * ownCloud Android client application + * + * @author masensio + * Copyright (C) 2015 ownCloud Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package com.owncloud.android.utils; + +import android.accounts.Account; +import android.os.AsyncTask; + +import com.owncloud.android.MainApp; +import com.owncloud.android.datamodel.FileDataStorageManager; +import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.lib.common.OwnCloudAccount; +import com.owncloud.android.lib.common.OwnCloudClient; +import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; +import com.owncloud.android.lib.common.operations.RemoteOperationResult; +import com.owncloud.android.lib.common.utils.Log_OC; +import com.owncloud.android.lib.resources.shares.OCShare; +import com.owncloud.android.operations.GetSharesForFileOperation; + +import java.lang.ref.WeakReference; +import java.util.ArrayList; + +/** + * Async Task to get the users and groups which a file is shared with + */ +public class GetShareWithUsersAsyncTask extends AsyncTask { + + private final String TAG = GetShareWithUsersAsyncTask.class.getSimpleName(); + private final WeakReference mListener; + private ArrayList mShares; + + public ArrayList getShares(){ + return mShares; + } + + public GetShareWithUsersAsyncTask(OnGetSharesWithUsersTaskListener listener) { + mListener = new WeakReference(listener); + } + + @Override + protected RemoteOperationResult doInBackground(Object... params) { + + RemoteOperationResult result = null; + + if (params != null && params.length == 3) { + OCFile file = (OCFile) params[0]; + Account account = (Account) params[1]; + FileDataStorageManager fileDataStorageManager = (FileDataStorageManager) params[2]; + + try { + // Get shares request + GetSharesForFileOperation operation = + new GetSharesForFileOperation(file.getRemotePath(), false, false); + OwnCloudAccount ocAccount = new OwnCloudAccount(account, + MainApp.getAppContext()); + OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton(). + getClientFor(ocAccount, MainApp.getAppContext()); + result = operation.execute(client, fileDataStorageManager); + + } catch (Exception e) { + result = new RemoteOperationResult(e); + Log_OC.e(TAG, "Exception while getting shares", e); + } + } else { + result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR); + } + + return result; + } + + @Override + protected void onPostExecute(RemoteOperationResult result) { + + if (result!= null) + { + OnGetSharesWithUsersTaskListener listener = mListener.get(); + if (listener!= null) + { + listener.onGetDataShareWithFinish(result); + } + } + } + + /* + * Interface to retrieve data from get shares task + */ + public interface OnGetSharesWithUsersTaskListener{ + + void onGetDataShareWithFinish(RemoteOperationResult result); + } +} \ No newline at end of file