From: masensio Date: Tue, 20 Oct 2015 13:10:40 +0000 (+0200) Subject: Use UnshareOperation instead an AsyncTask, for unshare with X-Git-Tag: oc-android-1.9^2~30^2~37 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/9cdaddefa33a73b15e0845e7db6111b0d96a153c?ds=inline Use UnshareOperation instead an AsyncTask, for unshare with --- diff --git a/src/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/com/owncloud/android/datamodel/FileDataStorageManager.java index 1e3e54e8..f0877d66 100644 --- a/src/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -202,7 +202,8 @@ public class FileDataStorageManager { cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict()); boolean sameRemotePath = fileExists(file.getRemotePath()); - if (sameRemotePath || fileExists(file.getFileId())) { // for renamed files; no more delete and create + if (sameRemotePath || + fileExists(file.getFileId())) { // for renamed files; no more delete and create OCFile oldFile; if (sameRemotePath) { @@ -437,9 +438,12 @@ public class FileDataStorageManager { } else { if (removeDBData) { - //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId()); - Uri file_uri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, file.getFileId()); - String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?" + " AND " + ProviderTableMeta.FILE_PATH + "=?"; + //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, + // ""+file.getFileId()); + Uri file_uri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, + file.getFileId()); + String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?" + " AND " + + ProviderTableMeta.FILE_PATH + "=?"; String[] whereArgs = new String[]{mAccount.name, file.getRemotePath()}; int deleted = 0; if (getContentProviderClient() != null) { @@ -568,7 +572,8 @@ public class FileDataStorageManager { OCFile targetParent = getFileByPath(targetParentPath); if (targetParent == null) { - throw new IllegalStateException("Parent folder of the target path does not exist!!"); + throw new IllegalStateException( + "Parent folder of the target path does not exist!!"); } /// 1. get all the descendants of the moved element in a single QUERY @@ -661,7 +666,8 @@ public class FileDataStorageManager { } } catch (Exception e) { - Log_OC.e(TAG, "Fail to update " + file.getFileId() + " and descendants in database", e); + Log_OC.e(TAG, "Fail to update " + file.getFileId() + " and descendants in database", + e); } /// 4. move in local file system @@ -931,7 +937,7 @@ public class FileDataStorageManager { cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getIdRemoteShared()); cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name); - if (shareExists(share.getIdRemoteShared())) { // for renamed files; no more delete and create + if (shareExists(share.getIdRemoteShared())) {// for renamed files; no more delete and create overriden = true; if (getContentResolver() != null) { getContentResolver().update(ProviderTableMeta.CONTENT_URI_SHARE, cv, @@ -1022,6 +1028,8 @@ public class FileDataStorageManager { .getColumnIndex(ProviderTableMeta.OCSHARES_ITEM_SOURCE))); share.setShareType(ShareType.fromValue(c.getInt(c .getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_TYPE)))); + share.setShareWith(c.getString(c + .getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_WITH))); share.setPermissions(c.getInt(c .getColumnIndex(ProviderTableMeta.OCSHARES_PERMISSIONS))); share.setSharedDate(c.getLong(c @@ -1035,7 +1043,8 @@ public class FileDataStorageManager { share.setIsFolder(c.getInt( c.getColumnIndex(ProviderTableMeta.OCSHARES_IS_DIRECTORY)) == 1); share.setUserId(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_USER_ID))); - share.setIdRemoteShared(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED))); + share.setIdRemoteShared(c.getLong( + c.getColumnIndex(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED))); } return share; } @@ -1087,7 +1096,8 @@ public class FileDataStorageManager { } else { try { - getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs); + getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, + whereArgs); } catch (RemoteException e) { Log_OC.e(TAG, "Exception in resetShareFlagsInAllFiles" + e.getMessage()); } @@ -1108,7 +1118,8 @@ public class FileDataStorageManager { } else { try { - getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs); + getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, + whereArgs); } catch (RemoteException e) { Log_OC.e(TAG, "Exception in resetShareFlagsInFolder " + e.getMessage()); } @@ -1124,7 +1135,8 @@ public class FileDataStorageManager { } else { try { - getContentProviderClient().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs); + getContentProviderClient().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, + whereArgs); } catch (RemoteException e) { Log_OC.e(TAG, "Exception in cleanShares" + e.getMessage()); } @@ -1184,7 +1196,8 @@ public class FileDataStorageManager { " operations to FileContentProvider"); try { if (getContentResolver() != null) { - results = getContentResolver().applyBatch(MainApp.getAuthority(), operations); + results = getContentResolver().applyBatch(MainApp.getAuthority(), + operations); } else { results = getContentProviderClient().applyBatch(operations); } @@ -1294,8 +1307,8 @@ public class FileDataStorageManager { public void removeShare(OCShare share) { Uri share_uri = ProviderTableMeta.CONTENT_URI_SHARE; String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?" + " AND " + - ProviderTableMeta.FILE_PATH + "=?"; - String [] whereArgs = new String[]{mAccount.name, share.getPath()}; + ProviderTableMeta._ID + "=?"; + String [] whereArgs = new String[]{mAccount.name, Long.toString(share.getId())}; if (getContentProviderClient() != null) { try { getContentProviderClient().delete(share_uri, where, whereArgs); diff --git a/src/com/owncloud/android/files/FileOperationsHelper.java b/src/com/owncloud/android/files/FileOperationsHelper.java index 3b5e062b..495d7a31 100644 --- a/src/com/owncloud/android/files/FileOperationsHelper.java +++ b/src/com/owncloud/android/files/FileOperationsHelper.java @@ -207,16 +207,37 @@ public class FileOperationsHelper { public void unshareFileWithLink(OCFile file) { + // Unshare the file: Create the intent + Intent unshareService = new Intent(mFileActivity, OperationsService.class); + unshareService.setAction(OperationsService.ACTION_UNSHARE); + unshareService.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount()); + unshareService.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath()); + unshareService.putExtra(OperationsService.EXTRA_SHARE_TYPE, ShareType.PUBLIC_LINK); + unshareService.putExtra(OperationsService.EXTRA_SHARE_WITH, ""); + + unshareFile(unshareService); + } + + public void unshareFileWithUserOrGroup(OCFile file, ShareType shareType, String userOrGroup){ + + // Unshare the file: Create the intent + Intent unshareService = new Intent(mFileActivity, OperationsService.class); + unshareService.setAction(OperationsService.ACTION_UNSHARE); + unshareService.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount()); + unshareService.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath()); + unshareService.putExtra(OperationsService.EXTRA_SHARE_TYPE, shareType); + unshareService.putExtra(OperationsService.EXTRA_SHARE_WITH, userOrGroup); + + unshareFile(unshareService); + } + + + private void unshareFile(Intent unshareService){ if (isSharedSupported()) { // Unshare the file - Intent service = new Intent(mFileActivity, OperationsService.class); - service.setAction(OperationsService.ACTION_UNSHARE); - service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount()); - service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath()); - service.putExtra(OperationsService.EXTRA_SHARE_TYPE, ShareType.PUBLIC_LINK); - service.putExtra(OperationsService.EXTRA_SHARE_WITH, ""); - mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service); - + mWaitingForOpId = mFileActivity.getOperationsServiceBinder(). + queueNewOperation(unshareService); + mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext(). getString(R.string.wait_a_moment)); diff --git a/src/com/owncloud/android/ui/activity/FileActivity.java b/src/com/owncloud/android/ui/activity/FileActivity.java index a34c7ee0..92452395 100644 --- a/src/com/owncloud/android/ui/activity/FileActivity.java +++ b/src/com/owncloud/android/ui/activity/FileActivity.java @@ -43,7 +43,6 @@ import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; -import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; diff --git a/src/com/owncloud/android/ui/activity/ShareActivity.java b/src/com/owncloud/android/ui/activity/ShareActivity.java index d7c7261f..8cbc04e4 100644 --- a/src/com/owncloud/android/ui/activity/ShareActivity.java +++ b/src/com/owncloud/android/ui/activity/ShareActivity.java @@ -21,22 +21,20 @@ package com.owncloud.android.ui.activity; -import android.accounts.Account; import android.app.SearchManager; import android.content.Intent; import android.net.Uri; import android.os.Bundle; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; -import android.support.v7.app.AppCompatActivity; import android.widget.Toast; import com.owncloud.android.R; -import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.providers.UsersAndGroupsSearchProvider; -import com.owncloud.android.ui.dialog.LoadingDialog; + +import com.owncloud.android.lib.common.operations.RemoteOperation; +import com.owncloud.android.lib.common.operations.RemoteOperationResult; +import com.owncloud.android.operations.UnshareOperation; import com.owncloud.android.ui.fragment.SearchFragment; import com.owncloud.android.ui.fragment.ShareFileFragment; @@ -166,6 +164,24 @@ public class ShareActivity extends FileActivity } } + /** + * Updates the view associated to the activity after the finish of some operation over files + * in the current account. + * + * @param operation Removal operation performed. + * @param result Result of the removal. + */ + @Override + public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) { + super.onRemoteOperationFinish(operation, result); + if (operation instanceof UnshareOperation) { + if (mShareFileFragment != null){ + mShareFileFragment.refreshUsersOrGroupsList(); + } + } + + } + @Override public void onShareFragmentInteraction(Uri uri) { diff --git a/src/com/owncloud/android/ui/fragment/ShareFileFragment.java b/src/com/owncloud/android/ui/fragment/ShareFileFragment.java index ed81a4a6..13c17c5f 100644 --- a/src/com/owncloud/android/ui/fragment/ShareFileFragment.java +++ b/src/com/owncloud/android/ui/fragment/ShareFileFragment.java @@ -34,20 +34,17 @@ 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.ui.activity.ShareActivity; +import com.owncloud.android.ui.activity.FileActivity; import com.owncloud.android.ui.adapter.ShareUserListAdapter; import com.owncloud.android.utils.DisplayUtils; import com.owncloud.android.utils.MimetypeIconUtil; -import com.owncloud.android.utils.UnshareWithUserAsyncTask; import java.util.ArrayList; @@ -61,8 +58,7 @@ import java.util.ArrayList; * Use the {@link ShareFileFragment#newInstance} factory method to * create an instance of this fragment. */ -public class ShareFileFragment extends Fragment - implements UnshareWithUserAsyncTask.OnUnshareWithUserTaskListener{ +public class ShareFileFragment extends Fragment { private static final String TAG = ShareFileFragment.class.getSimpleName(); @@ -137,12 +133,6 @@ public class ShareFileFragment extends Fragment size.setText(DisplayUtils.bytesToHumanReadable(mFile.getFileLength())); } - // List of share with users - TextView noShares = (TextView) view.findViewById(R.id.shareNoUsers); - - // TODO: Get shares from DB and show - - // Add User Button Button addUserGroupButton = (Button) view.findViewById(R.id.addUserButton); @@ -161,7 +151,7 @@ public class ShareFileFragment extends Fragment public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - getShares(); + refreshUsersOrGroupsList(); } // TODO: Rename method, update argument and hook method into UI event @@ -189,11 +179,14 @@ public class ShareFileFragment extends Fragment } // Get users and groups to fill the "share with" list - private void getShares(){ + public void refreshUsersOrGroupsList(){ + mShares = new ArrayList<>(); + // Get Users and Groups FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(mAccount, getActivity().getContentResolver()); - mShares = fileDataStorageManager.getSharesWithForAFile(mFile.getRemotePath(), mAccount.name); + mShares = fileDataStorageManager.getSharesWithForAFile(mFile.getRemotePath(), + mAccount.name); // Update list of users/groups updateListOfUserGroups(); @@ -235,7 +228,7 @@ public class ShareFileFragment extends Fragment @Override public void onClick(View v) { // Unshare - unshareWith((int)(mShares.get(position).getIdRemoteShared())); + unshareWith(mShares.get(position)); Log_OC.d(TAG, "Unshare - " + mShares.get(position).getSharedWithDisplayName()); } @@ -251,32 +244,16 @@ public class ShareFileFragment extends Fragment }); } - private void unshareWith(int shareId){ - ( (ShareActivity) getActivity()).showLoadingDialog(getActivity().getApplicationContext(). - getString(R.string.common_loading)); - // Remove Share with id - UnshareWithUserAsyncTask unshareTask = new UnshareWithUserAsyncTask(this); - FileDataStorageManager fileDataStorageManager = - new FileDataStorageManager(mAccount, getActivity().getContentResolver()); - Object[] params = { shareId, mAccount, fileDataStorageManager}; - unshareTask.execute(params); - } + private void unshareWith(OCShare share){ + OCFile file = ((FileActivity) getActivity()).getFile(); - @Override - public void onUnshareWithFinish(RemoteOperationResult result) { - // Remove loading - ((ShareActivity) getActivity()).dismissLoadingDialog(); - - if (result != null && result.isSuccess()) { - // Refresh data - //TODO: Refresh file or delete the user from the list - updateListOfUserGroups(); - - } else { - Toast.makeText(getActivity(), result.getLogMessage(), Toast.LENGTH_SHORT).show(); - } + ((FileActivity) getActivity()).getFileOperationsHelper(). + unshareFileWithUserOrGroup( + file, share.getShareType(), share.getShareWith() + ); } + // TODO: review if it is necessary /** * This interface must be implemented by activities that contain this diff --git a/src/com/owncloud/android/utils/GetShareWithUserAsyncTask.java b/src/com/owncloud/android/utils/GetShareWithUserAsyncTask.java deleted file mode 100644 index 8932df96..00000000 --- a/src/com/owncloud/android/utils/GetShareWithUserAsyncTask.java +++ /dev/null @@ -1,108 +0,0 @@ -/** - * 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 GetShareWithUserAsyncTask extends AsyncTask { - - private final String TAG = GetShareWithUserAsyncTask.class.getSimpleName(); - private final WeakReference mListener; - private ArrayList mShares; - - public ArrayList getShares(){ - return mShares; - } - - public GetShareWithUserAsyncTask(OnGetSharesWithUserTaskListener 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) - { - OnGetSharesWithUserTaskListener listener = mListener.get(); - if (listener!= null) - { - listener.onGetDataShareWithFinish(result); - } - } - } - - /* - * Interface to retrieve data from get shares task - */ - public interface OnGetSharesWithUserTaskListener{ - - void onGetDataShareWithFinish(RemoteOperationResult result); - } -} diff --git a/src/com/owncloud/android/utils/UnshareWithUserAsyncTask.java b/src/com/owncloud/android/utils/UnshareWithUserAsyncTask.java deleted file mode 100644 index 1eead208..00000000 --- a/src/com/owncloud/android/utils/UnshareWithUserAsyncTask.java +++ /dev/null @@ -1,104 +0,0 @@ -/** - * 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.lib.resources.shares.RemoveRemoteShareOperation; -import com.owncloud.android.operations.GetSharesForFileOperation; - -import java.lang.ref.WeakReference; -import java.util.ArrayList; - -/** - * Async Task to delete a share - */ -public class UnshareWithUserAsyncTask extends AsyncTask { - - private final String TAG = UnshareWithUserAsyncTask.class.getSimpleName(); - private final WeakReference mListener; - - public UnshareWithUserAsyncTask(OnUnshareWithUserTaskListener listener) { - mListener = new WeakReference(listener); - } - - @Override - protected RemoteOperationResult doInBackground(Object... params) { - - RemoteOperationResult result = null; - - if (params != null && params.length == 3) { - int shareId = (int) params[0]; - Account account = (Account) params[1]; - FileDataStorageManager fileDataStorageManager = (FileDataStorageManager) params[2]; - - try { - // Get shares request - RemoveRemoteShareOperation operation = - new RemoveRemoteShareOperation(shareId); - OwnCloudAccount ocAccount = new OwnCloudAccount(account, - MainApp.getAppContext()); - OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton(). - getClientFor(ocAccount, MainApp.getAppContext()); - result = operation.execute(client); - - } catch (Exception e) { - result = new RemoteOperationResult(e); - Log_OC.e(TAG, "Exception while unshare", e); - } - } else { - result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR); - } - - return result; - } - - @Override - protected void onPostExecute(RemoteOperationResult result) { - - if (result!= null) - { - OnUnshareWithUserTaskListener listener = mListener.get(); - if (listener!= null) - { - listener.onUnshareWithFinish(result); - } - } - } - - /* - * Interface to retrieve the result - */ - public interface OnUnshareWithUserTaskListener { - - void onUnshareWithFinish(RemoteOperationResult result); - } -}