Use UnshareOperation instead an AsyncTask, for unshare with

This commit is contained in:
masensio 2015-10-20 15:10:40 +02:00 committed by David A. Velasco
parent a00b272903
commit 9cdaddefa3
7 changed files with 94 additions and 280 deletions

View file

@ -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);

View file

@ -207,15 +207,36 @@ 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));

View file

@ -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;

View file

@ -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) {

View file

@ -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,31 +244,15 @@ 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();
((FileActivity) getActivity()).getFileOperationsHelper().
unshareFileWithUserOrGroup(
file, share.getShareType(), share.getShareWith()
);
}
@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();
}
}
// TODO: review if it is necessary
/**

View file

@ -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 <http://www.gnu.org/licenses/>.
*
*/
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<Object, Void, RemoteOperationResult> {
private final String TAG = GetShareWithUserAsyncTask.class.getSimpleName();
private final WeakReference<OnGetSharesWithUserTaskListener> mListener;
private ArrayList<OCShare> mShares;
public ArrayList<OCShare> getShares(){
return mShares;
}
public GetShareWithUserAsyncTask(OnGetSharesWithUserTaskListener listener) {
mListener = new WeakReference<OnGetSharesWithUserTaskListener>(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);
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*
*/
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<Object, Void, RemoteOperationResult> {
private final String TAG = UnshareWithUserAsyncTask.class.getSimpleName();
private final WeakReference<OnUnshareWithUserTaskListener> mListener;
public UnshareWithUserAsyncTask(OnUnshareWithUserTaskListener listener) {
mListener = new WeakReference<OnUnshareWithUserTaskListener>(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);
}
}