From: masensio Date: Fri, 7 Feb 2014 09:58:21 +0000 (+0100) Subject: Merge pull request #390 from owncloud/share_link__new_share X-Git-Tag: oc-android-1.5.5~58 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/1c83e751156e4986a2ddd06cf0a93893e9e36e71?hp=a9bf3f334dfa93e91860a04cabdf1d3192e2902e Merge pull request #390 from owncloud/share_link__new_share Share a file with a public link --- diff --git a/AndroidManifest.xml b/AndroidManifest.xml index ca11c81b..465dec84 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -54,7 +54,6 @@ > - @@ -81,9 +80,12 @@ - + - + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/res/layout/activity_row.xml b/res/layout/activity_row.xml new file mode 100644 index 00000000..b917600c --- /dev/null +++ b/res/layout/activity_row.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/res/values/setup.xml b/res/values/setup.xml index a40d5c21..2e07ae4b 100644 --- a/res/values/setup.xml +++ b/res/values/setup.xml @@ -8,6 +8,7 @@ ownCloud owncloud Owncloud_ + / diff --git a/src/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/com/owncloud/android/datamodel/FileDataStorageManager.java index 76e3e5c8..d1e26034 100644 --- a/src/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -29,6 +29,7 @@ import com.owncloud.android.MainApp; import com.owncloud.android.db.ProviderMeta.ProviderTableMeta; import com.owncloud.android.lib.operations.common.OCShare; import com.owncloud.android.lib.operations.common.ShareType; +import com.owncloud.android.lib.utils.FileUtils; import com.owncloud.android.utils.FileStorageUtils; import com.owncloud.android.utils.Log_OC; @@ -789,33 +790,23 @@ 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()); - cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isDirectory() ? 1 : 0); + 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); - boolean samePath = shareExists(share.getPath()); - if (samePath || - shareExists(share.getId())) { // for renamed files; no more delete and create - - OCShare oldFile = null; - if (samePath) { - oldFile = getShareByPath(share.getPath()); - share.setId(oldFile.getId()); - } else { - oldFile = getShareById(share.getId()); - } + if (shareExists(share.getIdRemoteShared())) { // for renamed files; no more delete and create overriden = true; if (getContentResolver() != null) { getContentResolver().update(ProviderTableMeta.CONTENT_URI_SHARE, cv, - ProviderTableMeta._ID + "=?", - new String[] { String.valueOf(share.getId()) }); + ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?", + new String[] { String.valueOf(share.getIdRemoteShared()) }); } else { try { getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_SHARE, - cv, ProviderTableMeta._ID + "=?", - new String[] { String.valueOf(share.getId()) }); + cv, ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?", + new String[] { String.valueOf(share.getIdRemoteShared()) }); } catch (RemoteException e) { Log_OC.e(TAG, "Fail to insert insert file to database " @@ -857,6 +848,16 @@ public class FileDataStorageManager { return share; } + private OCShare getShareByRemoteId(long remoteId) { + Cursor c = getShareCursorForValue(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, String.valueOf(remoteId)); + OCShare share = null; + if (c.moveToFirst()) { + share = createShareInstance(c); + } + c.close(); + return share; + } + public OCShare getShareByPath(String path) { Cursor c = getShareCursorForValue(ProviderTableMeta.OCSHARES_PATH, path); OCShare share = null; @@ -887,7 +888,7 @@ public class FileDataStorageManager { .getColumnIndex(ProviderTableMeta.OCSHARES_TOKEN))); share.setSharedWithDisplayName(c.getString(c .getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME))); - share.setIsDirectory(c.getInt( + share.setIsFolder(c.getInt( c.getColumnIndex(ProviderTableMeta.OCSHARES_IS_DIRECTORY)) == 1 ? true : false); share.setUserId(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_USER_ID))); share.setIdRemoteShared(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED))); @@ -926,14 +927,10 @@ public class FileDataStorageManager { return retval; } - public boolean shareExists(long id) { - return shareExists(ProviderTableMeta._ID, String.valueOf(id)); + private boolean shareExists(long remoteId) { + return shareExists(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, String.valueOf(remoteId)); } - public boolean shareExists(String path) { - return shareExists(ProviderTableMeta.OCSHARES_PATH, path); - } - private void cleanSharedFiles() { ContentValues cv = new ContentValues(); cv.put(ProviderTableMeta.FILE_SHARE_BY_LINK, false); @@ -954,6 +951,26 @@ public class FileDataStorageManager { } } + private void cleanSharedFilesInFolder(OCFile folder) { + ContentValues cv = new ContentValues(); + cv.put(ProviderTableMeta.FILE_SHARE_BY_LINK, false); + cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, ""); + 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 cleanSharedFilesInFolder " + e.getMessage()); + } + } + } + private void cleanShares() { String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?"; String [] whereArgs = new String[]{mAccount.name}; @@ -989,18 +1006,17 @@ 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()); - cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isDirectory() ? 1 : 0); + 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); - boolean existsByPath = shareExists(share.getPath()); - if (existsByPath || shareExists(share.getId())) { + if (shareExists(share.getIdRemoteShared())) { // updating an existing file operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE). withValues(cv). - withSelection( ProviderTableMeta._ID + "=?", - new String[] { String.valueOf(share.getId()) }) + withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?", + new String[] { String.valueOf(share.getIdRemoteShared()) }) .build()); } else { @@ -1099,4 +1115,151 @@ public class FileDataStorageManager { } } + + + public void saveSharesDB(ArrayList shares) { + saveShares(shares); + + 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 ¿and publicLink? + OCFile file = getFileByPath(path); + if (file != null) { + if (share.getShareType().equals(ShareType.PUBLIC_LINK)) { + file.setShareByLink(true); + sharedFiles.add(file); + } + } + } + + updateSharedFiles(sharedFiles); + } + + + public void saveSharesInFolder(ArrayList shares, OCFile folder) { + cleanSharedFilesInFolder(folder); + ArrayList operations = new ArrayList(); + operations = prepareRemoveSharesInFolder(folder, 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); + + /* + if (shareExists(share.getIdRemoteShared())) { + // updating an existing share resource + operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE). + withValues(cv). + withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?", + new String[] { String.valueOf(share.getIdRemoteShared()) }) + .build()); + + } else { + */ + // adding a new share resource + operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE).withValues(cv).build()); + //} + } + } + + // apply operations in batch + if (operations.size() > 0) { + @SuppressWarnings("unused") + ContentProviderResult[] results = null; + Log_OC.d(TAG, "Sending " + operations.size() + " operations to FileContentProvider"); + try { + if (getContentResolver() != null) { + results = getContentResolver().applyBatch(MainApp.getAuthority(), operations); + + } else { + results = 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()); + } + } + //} + + } + + private ArrayList prepareRemoveSharesInFolder(OCFile folder, ArrayList preparedOperations) { + if (folder != null) { + String where = ProviderTableMeta.OCSHARES_PATH + "=?" + " AND " + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?"; + String [] whereArgs = new String[]{ "", mAccount.name }; + + Vector files = getFolderContent(folder); + + for (OCFile file : files) { + whereArgs[0] = file.getRemotePath(); + preparedOperations.add(ContentProviderOperation.newDelete(ProviderTableMeta.CONTENT_URI_SHARE) + .withSelection(where, whereArgs) + .build()); + } + } + return preparedOperations; + + /* + if (operations.size() > 0) { + 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()); + } + } + */ + + /* + if (getContentResolver() != null) { + + getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE, + where, + whereArgs); + } else { + try { + getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE, + where, + whereArgs); + + } catch (RemoteException e) { + Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage()); + } + } + */ + //} + } } diff --git a/src/com/owncloud/android/files/FileHandler.java b/src/com/owncloud/android/files/FileHandler.java deleted file mode 100644 index 2eb754dd..00000000 --- a/src/com/owncloud/android/files/FileHandler.java +++ /dev/null @@ -1,30 +0,0 @@ -/* ownCloud Android client application - * Copyright (C) 2012-2013 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.files; - -import com.owncloud.android.datamodel.OCFile; - -public interface FileHandler { - - /** - * TODO - */ - public void openFile(OCFile file); - - -} diff --git a/src/com/owncloud/android/files/FileOperationsHelper.java b/src/com/owncloud/android/files/FileOperationsHelper.java new file mode 100644 index 00000000..ab8395cd --- /dev/null +++ b/src/com/owncloud/android/files/FileOperationsHelper.java @@ -0,0 +1,143 @@ +/* ownCloud Android client application + * Copyright (C) 2012-2014 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.files; + +import org.apache.http.protocol.HTTP; + +import android.accounts.AccountManager; +import android.content.Intent; +import android.net.Uri; +import android.support.v4.app.DialogFragment; +import android.webkit.MimeTypeMap; +import android.widget.Toast; + +import com.owncloud.android.R; +import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.lib.accounts.OwnCloudAccount; +import com.owncloud.android.lib.network.webdav.WebdavUtils; +import com.owncloud.android.lib.operations.common.ShareType; +import com.owncloud.android.operations.CreateShareOperation; +import com.owncloud.android.ui.activity.FileActivity; +import com.owncloud.android.ui.dialog.ActivityChooserDialog; +import com.owncloud.android.utils.Log_OC; + +/** + * + * @author masensio + * @author David A. Velasco + */ +public class FileOperationsHelper { + + private static final String TAG = FileOperationsHelper.class.getName(); + + private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG"; + + + public void openFile(OCFile file, FileActivity callerActivity) { + if (file != null) { + String storagePath = file.getStoragePath(); + String encodedStoragePath = WebdavUtils.encodePath(storagePath); + + Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW); + intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype()); + intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + + Intent intentForGuessedMimeType = null; + if (storagePath.lastIndexOf('.') >= 0) { + String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1)); + if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) { + intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW); + intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType); + intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + } + } + + Intent chooserIntent = null; + if (intentForGuessedMimeType != null) { + chooserIntent = Intent.createChooser(intentForGuessedMimeType, callerActivity.getString(R.string.actionbar_open_with)); + } else { + chooserIntent = Intent.createChooser(intentForSavedMimeType, callerActivity.getString(R.string.actionbar_open_with)); + } + + callerActivity.startActivity(chooserIntent); + + } else { + Log_OC.wtf(TAG, "Trying to open a NULL OCFile"); + } + } + + + public void shareFileWithLink(OCFile file, FileActivity callerActivity) { + + if (isSharedSupported(callerActivity)) { + if (file != null) { + String link = "https://fake.url"; + Intent intent = createShareWithLinkIntent(link); + String[] packagesToExclude = new String[] { callerActivity.getPackageName() }; + DialogFragment chooserDialog = ActivityChooserDialog.newInstance(intent, packagesToExclude, file); + chooserDialog.show(callerActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG); + + } else { + Log_OC.wtf(TAG, "Trying to share a NULL OCFile"); + } + + } else { + // Show a Message + Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG); + t.show(); + } + } + + + public void shareFileWithLinkToApp(OCFile file, Intent sendIntent, FileActivity callerActivity) { + + if (file != null) { + callerActivity.showLoadingDialog(); + CreateShareOperation createShare = new CreateShareOperation(file.getRemotePath(), ShareType.PUBLIC_LINK, "", false, "", 1, sendIntent); + createShare.execute(callerActivity.getStorageManager(), + callerActivity, + callerActivity.getRemoteOperationListener(), + callerActivity.getHandler(), + callerActivity); + + } else { + Log_OC.wtf(TAG, "Trying to open a NULL OCFile"); + } + } + + + private Intent createShareWithLinkIntent(String link) { + Intent intentToShareLink = new Intent(Intent.ACTION_SEND); + intentToShareLink.putExtra(Intent.EXTRA_TEXT, link); + intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE); + return intentToShareLink; + } + + + /** + * @return 'True' if the server supports the Share API + */ + public boolean isSharedSupported(FileActivity callerActivity) { + if (callerActivity.getAccount() != null) { + AccountManager accountManager = AccountManager.get(callerActivity); + return Boolean.parseBoolean(accountManager.getUserData(callerActivity.getAccount(), OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API)); + } + return false; + } + +} diff --git a/src/com/owncloud/android/operations/CreateShareOperation.java b/src/com/owncloud/android/operations/CreateShareOperation.java new file mode 100644 index 00000000..bf462a2c --- /dev/null +++ b/src/com/owncloud/android/operations/CreateShareOperation.java @@ -0,0 +1,129 @@ +/* ownCloud Android client application + * Copyright (C) 2014 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.operations; + +/** + * Creates a new share from a given file + * + * @author masensio + * + */ + +import android.content.Intent; + +import com.owncloud.android.datamodel.FileDataStorageManager; +import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.lib.network.OwnCloudClient; +import com.owncloud.android.lib.operations.common.OCShare; +import com.owncloud.android.lib.operations.common.RemoteOperationResult; +import com.owncloud.android.lib.operations.common.ShareType; +import com.owncloud.android.lib.operations.remote.CreateShareRemoteOperation; +import com.owncloud.android.lib.utils.FileUtils; +import com.owncloud.android.operations.common.SyncOperation; +import com.owncloud.android.utils.Log_OC; + +public class CreateShareOperation extends SyncOperation { + + private static final String TAG = CreateShareOperation.class.getSimpleName(); + + protected FileDataStorageManager mStorageManager; + + private String mPath; + private ShareType mShareType; + private String mShareWith; + private boolean mPublicUpload; + private String mPassword; + private int mPermissions; + private Intent mSendIntent; + + /** + * Constructor + * @param path Full path of the file/folder being shared. Mandatory argument + * @param shareType ‘0’ = user, ‘1’ = group, ‘3’ = Public link. Mandatory argument + * @param shareWith User/group ID with who the file should be shared. This is mandatory for shareType of 0 or 1 + * @param publicUpload If ‘false’ (default) public cannot upload to a public shared folder. + * If ‘true’ public can upload to a shared folder. Only available for public link shares + * @param password Password to protect a public link share. Only available for public link shares + * @param permissions 1 - Read only – Default for “public” shares + * 2 - Update + * 4 - Create + * 8 - Delete + * 16- Re-share + * 31- All above – Default for “private” shares + * For user or group shares. + * To obtain combinations, add the desired values together. + * For instance, for “Re-Share”, “delete”, “read”, “update”, add 16+8+2+1 = 27. + */ + public CreateShareOperation(String path, ShareType shareType, String shareWith, boolean publicUpload, + String password, int permissions, Intent sendIntent) { + + mPath = path; + mShareType = shareType; + mShareWith = shareWith; + mPublicUpload = publicUpload; + mPassword = password; + mPermissions = permissions; + mSendIntent = sendIntent; + } + + @Override + protected RemoteOperationResult run(OwnCloudClient client) { + CreateShareRemoteOperation operation = new CreateShareRemoteOperation(mPath, mShareType, mShareWith, mPublicUpload, mPassword, mPermissions); + RemoteOperationResult result = operation.execute(client); + + if (result.isSuccess()) { + + if (result.getData().size() > 0) { + OCShare share = (OCShare) result.getData().get(0); + + // Update DB with the response + if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) { + share.setPath(mPath.substring(0, mPath.length()-1)); + share.setIsFolder(true); + + } else { + share.setPath(mPath); + share.setIsFolder(false); + } + share.setPermissions(mPermissions); + + getStorageManager().saveShare(share); + + // Update OCFile with data from share: ShareByLink and publicLink + OCFile file = getStorageManager().getFileByPath(mPath); + if (file!=null) { + mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink()); + file.setPublicLink(share.getShareLink()); + file.setShareByLink(true); + getStorageManager().saveFile(file); + Log_OC.d(TAG, "Public Link = " + file.getPublicLink()); + + } + } + } + + + return result; + } + + + public Intent getSendIntent() { + return mSendIntent; + } + +} diff --git a/src/com/owncloud/android/operations/GetSharesForFileOperation.java b/src/com/owncloud/android/operations/GetSharesForFileOperation.java new file mode 100644 index 00000000..fd2dab74 --- /dev/null +++ b/src/com/owncloud/android/operations/GetSharesForFileOperation.java @@ -0,0 +1,79 @@ +/* ownCloud Android client application + * Copyright (C) 2012-2013 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.operations; + +import java.util.ArrayList; + +import com.owncloud.android.lib.network.OwnCloudClient; +import com.owncloud.android.lib.operations.common.OCShare; +import com.owncloud.android.lib.operations.common.RemoteOperationResult; +import com.owncloud.android.lib.operations.remote.GetSharesForFileRemoteOperation; +import com.owncloud.android.operations.common.SyncOperation; +import com.owncloud.android.utils.Log_OC; + +/** + * Provide a list shares for a specific file. + * + * @author masensio + * + */ +public class GetSharesForFileOperation extends SyncOperation { + + private static final String TAG = GetSharesForFileOperation.class.getSimpleName(); + + private String mPath; + private boolean mReshares; + private boolean mSubfiles; + + /** + * Constructor + * + * @param path Path to file or folder + * @param reshares If set to ‘false’ (default), only shares from the current user are returned + * If set to ‘true’, all shares from the given file are returned + * @param subfiles If set to ‘false’ (default), lists only the folder being shared + * If set to ‘true’, all shared files within the folder are returned. + */ + public GetSharesForFileOperation(String path, boolean reshares, boolean subfiles) { + mPath = path; + mReshares = reshares; + mSubfiles = subfiles; + } + + @Override + protected RemoteOperationResult run(OwnCloudClient client) { + GetSharesForFileRemoteOperation operation = new GetSharesForFileRemoteOperation(mPath, mReshares, mSubfiles); + RemoteOperationResult result = operation.execute(client); + + if (result.isSuccess()) { + + // Update DB with the response + Log_OC.d(TAG, "File = " + mPath + " Share list size " + result.getData().size()); + ArrayList shares = new ArrayList(); + for(Object obj: result.getData()) { + shares.add((OCShare) obj); + } + + getStorageManager().saveSharesDB(shares); + } + + return result; + } + +} diff --git a/src/com/owncloud/android/operations/GetSharesOperation.java b/src/com/owncloud/android/operations/GetSharesOperation.java index 4cbee02a..8f7a2ae8 100644 --- a/src/com/owncloud/android/operations/GetSharesOperation.java +++ b/src/com/owncloud/android/operations/GetSharesOperation.java @@ -19,13 +19,10 @@ package com.owncloud.android.operations; import java.util.ArrayList; -import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.lib.network.OwnCloudClient; import com.owncloud.android.lib.operations.common.RemoteOperationResult; import com.owncloud.android.lib.operations.common.OCShare; -import com.owncloud.android.lib.operations.common.ShareType; import com.owncloud.android.lib.operations.remote.GetRemoteSharesOperation; -import com.owncloud.android.lib.utils.FileUtils; import com.owncloud.android.operations.common.SyncOperation; import com.owncloud.android.utils.Log_OC; @@ -55,36 +52,10 @@ public class GetSharesOperation extends SyncOperation { shares.add((OCShare) obj); } - saveSharesDB(shares); + getStorageManager().saveSharesDB(shares); } return result; } - private void saveSharesDB(ArrayList shares) { - // Save share file - getStorageManager().saveShares(shares); - - ArrayList sharedFiles = new ArrayList(); - - for (OCShare share : shares) { - // Get the path - String path = share.getPath(); - if (share.isDirectory()) { - path = path + FileUtils.PATH_SEPARATOR; - } - - // Update OCFile with data from share: ShareByLink ¿and publicLink? - OCFile file = getStorageManager().getFileByPath(path); - if (file != null) { - if (share.getShareType().equals(ShareType.PUBLIC_LINK)) { - file.setShareByLink(true); - sharedFiles.add(file); - } - } - } - - getStorageManager().updateSharedFiles(sharedFiles); - } - } diff --git a/src/com/owncloud/android/operations/SynchronizeFolderOperation.java b/src/com/owncloud/android/operations/SynchronizeFolderOperation.java index 97c7a083..11ce41e5 100644 --- a/src/com/owncloud/android/operations/SynchronizeFolderOperation.java +++ b/src/com/owncloud/android/operations/SynchronizeFolderOperation.java @@ -33,14 +33,16 @@ import org.apache.http.HttpStatus; import android.accounts.Account; import android.content.Context; import android.content.Intent; -import android.support.v4.content.LocalBroadcastManager; +//import android.support.v4.content.LocalBroadcastManager; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.lib.network.OwnCloudClient; +import com.owncloud.android.lib.operations.common.OCShare; import com.owncloud.android.lib.operations.common.RemoteOperation; import com.owncloud.android.lib.operations.common.RemoteOperationResult; import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode; +import com.owncloud.android.lib.operations.remote.GetSharesForFileRemoteOperation; import com.owncloud.android.lib.operations.remote.ReadRemoteFileOperation; import com.owncloud.android.lib.operations.remote.ReadRemoteFolderOperation; import com.owncloud.android.lib.operations.common.RemoteFile; @@ -65,7 +67,8 @@ public class SynchronizeFolderOperation extends RemoteOperation { private static final String TAG = SynchronizeFolderOperation.class.getSimpleName(); - public static final String EVENT_SINGLE_FOLDER_SYNCED = SynchronizeFolderOperation.class.getName() + ".EVENT_SINGLE_FOLDER_SYNCED"; + public static final String EVENT_SINGLE_FOLDER_CONTENTS_SYNCED = SynchronizeFolderOperation.class.getName() + ".EVENT_SINGLE_FOLDER_CONTENTS_SYNCED"; + public static final String EVENT_SINGLE_FOLDER_SHARES_SYNCED = SynchronizeFolderOperation.class.getName() + ".EVENT_SINGLE_FOLDER_SHARES_SYNCED"; /** Time stamp for the synchronization process in progress */ private long mCurrentSyncTime; @@ -97,9 +100,12 @@ public class SynchronizeFolderOperation extends RemoteOperation { /** 'True' means that this operation is part of a full account synchronization */ private boolean mSyncFullAccount; + /** 'True' means that Share resources bound to the files into the folder should be refreshed also */ + private boolean mRefreshShares; + /** 'True' means that the remote folder changed from last synchronization and should be fetched */ private boolean mRemoteFolderChanged; - + /** * Creates a new instance of {@link SynchronizeFolderOperation}. @@ -116,12 +122,14 @@ public class SynchronizeFolderOperation extends RemoteOperation { public SynchronizeFolderOperation( OCFile folder, long currentSyncTime, boolean syncFullAccount, + boolean refreshShares, FileDataStorageManager dataStorageManager, Account account, Context context ) { mLocalFolder = folder; mCurrentSyncTime = currentSyncTime; mSyncFullAccount = syncFullAccount; + mRefreshShares = refreshShares; mStorageManager = dataStorageManager; mAccount = account; mContext = context; @@ -174,46 +182,57 @@ public class SynchronizeFolderOperation extends RemoteOperation { } if (!mSyncFullAccount) { - sendLocalBroadcast(mLocalFolder.getRemotePath(), result); + sendLocalBroadcast(EVENT_SINGLE_FOLDER_CONTENTS_SYNCED, mLocalFolder.getRemotePath(), result); } - + + if (result.isSuccess() && mRefreshShares) { + RemoteOperationResult shareResult = refreshSharesForFolder(client); + if (shareResult.getCode() != ResultCode.FILE_NOT_FOUND) { + result = shareResult; + } // else , keep the previous result ; being conservative for servers where Sharing API is supported, but disabled + } + + if (!mSyncFullAccount) { + sendLocalBroadcast(EVENT_SINGLE_FOLDER_SHARES_SYNCED, mLocalFolder.getRemotePath(), result); + } + return result; } - private RemoteOperationResult checkForChanges(OwnCloudClient client) { mRemoteFolderChanged = false; RemoteOperationResult result = null; String remotePath = null; - remotePath = mLocalFolder.getRemotePath(); - Log_OC.d(TAG, "Checking changes in " + mAccount.name + remotePath); - - // remote request - ReadRemoteFileOperation operation = new ReadRemoteFileOperation(remotePath); - result = operation.execute(client); - if (result.isSuccess()){ - OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0)); - - // check if remote and local folder are different - mRemoteFolderChanged = !(remoteFolder.getEtag().equalsIgnoreCase(mLocalFolder.getEtag())); - - result = new RemoteOperationResult(ResultCode.OK); - - Log_OC.i(TAG, "Checked " + mAccount.name + remotePath + " : " + (mRemoteFolderChanged ? "changed" : "not changed")); + remotePath = mLocalFolder.getRemotePath(); + Log_OC.d(TAG, "Checking changes in " + mAccount.name + remotePath); + + // remote request + ReadRemoteFileOperation operation = new ReadRemoteFileOperation(remotePath); + result = operation.execute(client); + if (result.isSuccess()){ + OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0)); + + // check if remote and local folder are different + mRemoteFolderChanged = !(remoteFolder.getEtag().equalsIgnoreCase(mLocalFolder.getEtag())); + + result = new RemoteOperationResult(ResultCode.OK); + + Log_OC.i(TAG, "Checked " + mAccount.name + remotePath + " : " + (mRemoteFolderChanged ? "changed" : "not changed")); + + } else { + // check failed + if (result.getCode() == ResultCode.FILE_NOT_FOUND) { + removeLocalFolder(); + } + if (result.isException()) { + Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " + result.getLogMessage(), result.getException()); } else { - // check failed - if (result.getCode() == ResultCode.FILE_NOT_FOUND) { - removeLocalFolder(); - } - if (result.isException()) { - Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " + result.getLogMessage(), result.getException()); - } else { - Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " + result.getLogMessage()); - } + Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " + result.getLogMessage()); } - + } + return result; } @@ -332,13 +351,7 @@ public class SynchronizeFolderOperation extends RemoteOperation { // request for the synchronization of file contents AFTER saving current remote properties startContentSynchronizations(filesToSyncContents, client); - // removal of obsolete files - //removeObsoleteFiles(); - - // must be done AFTER saving all the children information, so that eTag is not updated in the database in case of unexpected exceptions - //mStorageManager.saveFile(remoteFolder); mChildren = updatedFiles; - } /** @@ -453,6 +466,27 @@ public class SynchronizeFolderOperation extends RemoteOperation { } } } + + + private RemoteOperationResult refreshSharesForFolder(OwnCloudClient client) { + RemoteOperationResult result = null; + + // remote request + GetSharesForFileRemoteOperation operation = new GetSharesForFileRemoteOperation(mLocalFolder.getRemotePath(), false, true); + result = operation.execute(client); + + if (result.isSuccess()) { + // update local database + ArrayList shares = new ArrayList(); + for(Object obj: result.getData()) { + shares.add((OCShare) obj); + } + mStorageManager.saveSharesInFolder(shares, mLocalFolder); + } + + return result; + } + /** * Scans the default location for saving local copies of files searching for @@ -475,18 +509,20 @@ public class SynchronizeFolderOperation extends RemoteOperation { /** * Sends a message to any application component interested in the progress of the synchronization. * - * @param inProgress 'True' when the synchronization progress is not finished. + * @param event * @param dirRemotePath Remote path of a folder that was just synchronized (with or without success) + * @param result */ - private void sendLocalBroadcast(String dirRemotePath, RemoteOperationResult result) { - Intent intent = new Intent(EVENT_SINGLE_FOLDER_SYNCED); + private void sendLocalBroadcast(String event, String dirRemotePath, RemoteOperationResult result) { + Log_OC.d(TAG, "Send broadcast " + event); + Intent intent = new Intent(event); intent.putExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME, mAccount.name); if (dirRemotePath != null) { intent.putExtra(FileSyncAdapter.EXTRA_FOLDER_PATH, dirRemotePath); } intent.putExtra(FileSyncAdapter.EXTRA_RESULT, result); - //mContext.sendStickyBroadcast(intent); - LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent); + mContext.sendStickyBroadcast(intent); + //LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent); } diff --git a/src/com/owncloud/android/providers/FileContentProvider.java b/src/com/owncloud/android/providers/FileContentProvider.java index 8bf258f4..36c03538 100644 --- a/src/com/owncloud/android/providers/FileContentProvider.java +++ b/src/com/owncloud/android/providers/FileContentProvider.java @@ -25,6 +25,7 @@ import com.owncloud.android.R; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.db.ProviderMeta; import com.owncloud.android.db.ProviderMeta.ProviderTableMeta; +import com.owncloud.android.lib.operations.common.ShareType; import com.owncloud.android.utils.Log_OC; @@ -298,23 +299,23 @@ public class FileContentProvider extends ContentProvider { String[] projectionShare = new String[] {ProviderTableMeta._ID, ProviderTableMeta.OCSHARES_PATH, ProviderTableMeta.OCSHARES_ACCOUNT_OWNER }; String whereShare = ProviderTableMeta.OCSHARES_PATH + "=? AND " + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?"; String[] whereArgsShare = new String[] {path, accountNameShare}; + Uri insertedShareUri = null; Cursor doubleCheckShare = query(db, uri, projectionShare, whereShare, whereArgsShare, null); if (doubleCheckShare == null || !doubleCheckShare.moveToFirst()) { // ugly patch; serious refactorization is needed to reduce work in FileDataStorageManager and bring it to FileContentProvider long rowId = db.insert(ProviderTableMeta.OCSHARES_TABLE_NAME, null, values); if (rowId >0) { - Uri insertedShareUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, rowId); - return insertedShareUri; + insertedShareUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, rowId); } else { throw new SQLException("ERROR " + uri); } } else { // file is already inserted; race condition, let's avoid a duplicated entry - Uri insertedFileUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, doubleCheckShare.getLong(doubleCheckShare.getColumnIndex(ProviderTableMeta._ID))); + insertedShareUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, doubleCheckShare.getLong(doubleCheckShare.getColumnIndex(ProviderTableMeta._ID))); doubleCheckShare.close(); - - return insertedFileUri; } + updateFilesTableAccordingToShareInsertion(db, uri, values); + return insertedShareUri; default: @@ -322,8 +323,20 @@ public class FileContentProvider extends ContentProvider { } } - + private void updateFilesTableAccordingToShareInsertion(SQLiteDatabase db, Uri uri, ContentValues shareValues) { + ContentValues fileValues = new ContentValues(); + fileValues.put(ProviderTableMeta.FILE_SHARE_BY_LINK, + ShareType.PUBLIC_LINK.getValue() == shareValues.getAsInteger(ProviderTableMeta.OCSHARES_SHARE_TYPE)? 1 : 0); + String whereShare = ProviderTableMeta.FILE_PATH + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?"; + String[] whereArgsShare = new String[] { + shareValues.getAsString(ProviderTableMeta.OCSHARES_PATH), + shareValues.getAsString(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER) + }; + db.update(ProviderTableMeta.FILE_TABLE_NAME, fileValues, whereShare, whereArgsShare); + } + + @Override public boolean onCreate() { mDbHelper = new DataBaseHelper(getContext()); diff --git a/src/com/owncloud/android/services/OperationsService.java b/src/com/owncloud/android/services/OperationsService.java index 316657c4..919bedb3 100644 --- a/src/com/owncloud/android/services/OperationsService.java +++ b/src/com/owncloud/android/services/OperationsService.java @@ -42,7 +42,7 @@ import android.os.IBinder; import android.os.Looper; import android.os.Message; import android.os.Process; -import android.support.v4.content.LocalBroadcastManager; +//import android.support.v4.content.LocalBroadcastManager; import android.util.Pair; public class OperationsService extends Service { @@ -228,7 +228,14 @@ public class OperationsService extends Service { Log_OC.e(TAG, "Error while trying to get autorization for " + mLastTarget.mAccount.name, e); } result = new RemoteOperationResult(e); - + } catch (Exception e) { + if (mLastTarget.mAccount == null) { + Log_OC.e(TAG, "Unexpected error for a NULL account", e); + } else { + Log_OC.e(TAG, "Unexpected error for " + mLastTarget.mAccount.name, e); + } + result = new RemoteOperationResult(e); + } finally { synchronized(mPendingOperations) { mPendingOperations.poll(); @@ -255,8 +262,9 @@ public class OperationsService extends Service { } else { intent.putExtra(EXTRA_SERVER_URL, target.mServerUrl); } - LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); - lbm.sendBroadcast(intent); + //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); + //lbm.sendBroadcast(intent); + sendStickyBroadcast(intent); } @@ -279,8 +287,9 @@ public class OperationsService extends Service { } else { intent.putExtra(EXTRA_SERVER_URL, target.mServerUrl); } - LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); - lbm.sendBroadcast(intent); + //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); + //lbm.sendBroadcast(intent); + sendStickyBroadcast(intent); } diff --git a/src/com/owncloud/android/syncadapter/FileSyncAdapter.java b/src/com/owncloud/android/syncadapter/FileSyncAdapter.java index 7b628c61..6b2ea73e 100644 --- a/src/com/owncloud/android/syncadapter/FileSyncAdapter.java +++ b/src/com/owncloud/android/syncadapter/FileSyncAdapter.java @@ -30,6 +30,7 @@ import com.owncloud.android.R; import com.owncloud.android.authentication.AuthenticatorActivity; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.lib.accounts.OwnCloudAccount; import com.owncloud.android.lib.operations.common.RemoteOperationResult; import com.owncloud.android.operations.SynchronizeFolderOperation; import com.owncloud.android.operations.UpdateOCVersionOperation; @@ -40,6 +41,7 @@ import com.owncloud.android.utils.Log_OC; import android.accounts.Account; +import android.accounts.AccountManager; import android.accounts.AccountsException; import android.app.Notification; import android.app.NotificationManager; @@ -51,7 +53,7 @@ import android.content.Context; import android.content.Intent; import android.content.SyncResult; import android.os.Bundle; -import android.support.v4.content.LocalBroadcastManager; +//import android.support.v4.content.LocalBroadcastManager; /** * Implementation of {@link AbstractThreadedSyncAdapter} responsible for synchronizing @@ -72,8 +74,8 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { public static final String EVENT_FULL_SYNC_START = FileSyncAdapter.class.getName() + ".EVENT_FULL_SYNC_START"; public static final String EVENT_FULL_SYNC_END = FileSyncAdapter.class.getName() + ".EVENT_FULL_SYNC_END"; - public static final String EVENT_FOLDER_CONTENTS_SYNCED = FileSyncAdapter.class.getName() + ".EVENT_FOLDER_CONTENTS_SYNCED"; - public static final String EVENT_FOLDER_SIZE_SYNCED = FileSyncAdapter.class.getName() + ".EVENT_FOLDER_SIZE_SYNCED"; + public static final String EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED = FileSyncAdapter.class.getName() + ".EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED"; + public static final String EVENT_FULL_SYNC_FOLDER_SIZE_SYNCED = FileSyncAdapter.class.getName() + ".EVENT_FULL_SYNC_FOLDER_SIZE_SYNCED"; public static final String EXTRA_ACCOUNT_NAME = FileSyncAdapter.class.getName() + ".EXTRA_ACCOUNT_NAME"; public static final String EXTRA_FOLDER_PATH = FileSyncAdapter.class.getName() + ".EXTRA_FOLDER_PATH"; @@ -106,6 +108,9 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { /** {@link SyncResult} instance to return to the system when the synchronization finish */ private SyncResult mSyncResult; + + /** 'True' means that the server supports the share API */ + private boolean mIsSharedSupported; /** @@ -150,6 +155,10 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { this.setAccount(account); this.setContentProviderClient(providerClient); this.setStorageManager(new FileDataStorageManager(account, providerClient)); + + AccountManager accountManager = getAccountManager(); + mIsSharedSupported = Boolean.parseBoolean(accountManager.getUserData(account, OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API)); + try { this.initClientForCurrentAccount(); } catch (IOException e) { @@ -254,13 +263,13 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { DataStorageManager dataStorageManager, Account account, Context context ) { - } */ // folder synchronization SynchronizeFolderOperation synchFolderOp = new SynchronizeFolderOperation( folder, mCurrentSyncTime, true, + mIsSharedSupported, getStorageManager(), getAccount(), getContext() @@ -269,7 +278,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { // synchronized folder -> notice to UI - ALWAYS, although !result.isSuccess - sendLocalBroadcast(EVENT_FOLDER_CONTENTS_SYNCED, folder.getRemotePath(), result); + sendLocalBroadcast(EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED, folder.getRemotePath(), result); // check the result of synchronizing the folder if (result.isSuccess() || result.getCode() == ResultCode.SYNC_CONFLICT) { @@ -343,7 +352,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { syncDown = (parentEtagChanged || etag == null || etag.length() == 0); if(syncDown) { */ synchronizeFolder(newFile); - sendLocalBroadcast(EVENT_FOLDER_SIZE_SYNCED, parent.getRemotePath(), null); + sendLocalBroadcast(EVENT_FULL_SYNC_FOLDER_SIZE_SYNCED, parent.getRemotePath(), null); //} } } @@ -360,6 +369,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { * @param result Result of an individual {@ SynchronizeFolderOperation}, if completed; may be null. */ private void sendLocalBroadcast(String event, String dirRemotePath, RemoteOperationResult result) { + Log_OC.d(TAG, "Send broadcast " + event); Intent intent = new Intent(event); intent.putExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME, getAccount().name); if (dirRemotePath != null) { @@ -368,8 +378,8 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { if (result != null) { intent.putExtra(FileSyncAdapter.EXTRA_RESULT, result); } - //getContext().sendStickyBroadcast(i); - LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent); + getContext().sendStickyBroadcast(intent); + //LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent); } diff --git a/src/com/owncloud/android/ui/activity/ConflictsResolveActivity.java b/src/com/owncloud/android/ui/activity/ConflictsResolveActivity.java index 5b22c341..fbbc5dff 100644 --- a/src/com/owncloud/android/ui/activity/ConflictsResolveActivity.java +++ b/src/com/owncloud/android/ui/activity/ConflictsResolveActivity.java @@ -19,7 +19,6 @@ package com.owncloud.android.ui.activity; import com.actionbarsherlock.app.ActionBar; -import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.services.FileUploader; import com.owncloud.android.ui.dialog.ConflictsResolveDialog; @@ -77,6 +76,7 @@ public class ConflictsResolveActivity extends FileActivity implements OnConflict @Override protected void onAccountSet(boolean stateWasRecovered) { + super.onAccountSet(stateWasRecovered); if (getAccount() != null) { OCFile file = getFile(); if (getFile() == null) { @@ -84,8 +84,7 @@ public class ConflictsResolveActivity extends FileActivity implements OnConflict finish(); } else { /// Check whether the 'main' OCFile handled by the Activity is contained in the current Account - FileDataStorageManager storageManager = new FileDataStorageManager(getAccount(), getContentResolver()); - file = storageManager.getFileByPath(file.getRemotePath()); // file = null if not in the current Account + file = getStorageManager().getFileByPath(file.getRemotePath()); // file = null if not in the current Account if (file != null) { setFile(file); ConflictsResolveDialog d = ConflictsResolveDialog.newInstance(file.getRemotePath(), this); @@ -98,7 +97,6 @@ public class ConflictsResolveActivity extends FileActivity implements OnConflict } } else { - Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!"); finish(); } diff --git a/src/com/owncloud/android/ui/activity/FileActivity.java b/src/com/owncloud/android/ui/activity/FileActivity.java index 3a9297ec..3c8a2011 100644 --- a/src/com/owncloud/android/ui/activity/FileActivity.java +++ b/src/com/owncloud/android/ui/activity/FileActivity.java @@ -24,19 +24,27 @@ import android.accounts.AccountManagerCallback; import android.accounts.AccountManagerFuture; import android.accounts.OperationCanceledException; import android.content.Intent; -import android.net.Uri; import android.os.Bundle; -import android.webkit.MimeTypeMap; +import android.os.Handler; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentTransaction; +import android.widget.Toast; import com.actionbarsherlock.app.SherlockFragmentActivity; import com.owncloud.android.MainApp; import com.owncloud.android.R; import com.owncloud.android.authentication.AccountUtils; +import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.files.FileOperationsHelper; +import com.owncloud.android.lib.operations.common.OnRemoteOperationListener; +import com.owncloud.android.lib.operations.common.RemoteOperation; +import com.owncloud.android.lib.operations.common.RemoteOperationResult; +import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode; +import com.owncloud.android.operations.CreateShareOperation; -import com.owncloud.android.lib.accounts.OwnCloudAccount; -import com.owncloud.android.lib.network.webdav.WebdavUtils; - +import com.owncloud.android.ui.dialog.LoadingDialog; import com.owncloud.android.utils.Log_OC; @@ -45,14 +53,16 @@ import com.owncloud.android.utils.Log_OC; * * @author David A. Velasco */ -public abstract class FileActivity extends SherlockFragmentActivity { +public class FileActivity extends SherlockFragmentActivity implements OnRemoteOperationListener { public static final String EXTRA_FILE = "com.owncloud.android.ui.activity.FILE"; public static final String EXTRA_ACCOUNT = "com.owncloud.android.ui.activity.ACCOUNT"; public static final String EXTRA_WAITING_TO_PREVIEW = "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW"; public static final String EXTRA_FROM_NOTIFICATION= "com.owncloud.android.ui.activity.FROM_NOTIFICATION"; - public static final String TAG = FileActivity.class.getSimpleName(); + public static final String TAG = FileActivity.class.getSimpleName(); + + private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT"; /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */ @@ -73,6 +83,13 @@ public abstract class FileActivity extends SherlockFragmentActivity { /** Flag to signal if the activity is launched by a notification */ private boolean mFromNotification; + /** Messages handler associated to the main thread and the life cycle of the activity */ + private Handler mHandler; + + /** Access point to the cached database for the current ownCloud {@link Account} */ + private FileDataStorageManager mStorageManager = null; + + private FileOperationsHelper mFileOperationsHelper; /** @@ -85,6 +102,8 @@ public abstract class FileActivity extends SherlockFragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + mHandler = new Handler(); + mFileOperationsHelper = new FileOperationsHelper(); Account account; if(savedInstanceState != null) { account = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT); @@ -250,17 +269,6 @@ public abstract class FileActivity extends SherlockFragmentActivity { /** - * @return 'True' if the server supports the Share API - */ - public boolean isSharedSupported() { - if (getAccount() != null) { - AccountManager accountManager = AccountManager.get(this); - return Boolean.parseBoolean(accountManager.getUserData(getAccount(), OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API)); - } - return false; - } - - /** * Helper class handling a callback from the {@link AccountManager} after the creation of * a new ownCloud {@link Account} finished, successfully or not. * @@ -307,40 +315,85 @@ public abstract class FileActivity extends SherlockFragmentActivity { * * Child classes must grant that state depending on the {@link Account} is updated. */ - protected abstract void onAccountSet(boolean stateWasRecovered); + protected void onAccountSet(boolean stateWasRecovered) { + if (getAccount() != null) { + mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver()); + + } else { + Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!"); + } + } + + + public FileDataStorageManager getStorageManager() { + return mStorageManager; + } + + + public OnRemoteOperationListener getRemoteOperationListener() { + return this; + } + + + public Handler getHandler() { + return mHandler; + } + public FileOperationsHelper getFileOperationsHelper() { + return mFileOperationsHelper; + } + /** + * + * @param operation Removal operation performed. + * @param result Result of the removal. + */ + @Override + public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) { + Log_OC.d(TAG, "Received result of operation in FileActivity - common behaviour for all the FileActivities "); + if (operation instanceof CreateShareOperation) { + onCreateShareOperationFinish((CreateShareOperation) operation, result); + } + } - public void openFile(OCFile file) { - if (file != null) { - String storagePath = file.getStoragePath(); - String encodedStoragePath = WebdavUtils.encodePath(storagePath); - - Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW); - intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype()); - intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); - - Intent intentForGuessedMimeType = null; - if (storagePath.lastIndexOf('.') >= 0) { - String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1)); - if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) { - intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW); - intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType); - intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); - } - } - - Intent chooserIntent = null; - if (intentForGuessedMimeType != null) { - chooserIntent = Intent.createChooser(intentForGuessedMimeType, getString(R.string.actionbar_open_with)); - } else { - chooserIntent = Intent.createChooser(intentForSavedMimeType, getString(R.string.actionbar_open_with)); - } - - startActivity(chooserIntent); + private void onCreateShareOperationFinish(CreateShareOperation operation, RemoteOperationResult result) { + dismissLoadingDialog(); + if (result.isSuccess()) { + Intent sendIntent = operation.getSendIntent(); + startActivity(sendIntent); - } else { - Log_OC.wtf(TAG, "Trying to open a NULL OCFile"); + } else if (result.getCode() == ResultCode.FILE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND + Toast t = Toast.makeText(this, getString(R.string.share_link_file_no_exist), Toast.LENGTH_LONG); + t.show(); + } else { // Generic error + // Show a Message, operation finished without success + Toast t = Toast.makeText(this, getString(R.string.share_link_file_error), Toast.LENGTH_LONG); + t.show(); + } + } + + + /** + * Show loading dialog + */ + public void showLoadingDialog() { + // Construct dialog + LoadingDialog loading = new LoadingDialog(getResources().getString(R.string.wait_a_moment)); + FragmentManager fm = getSupportFragmentManager(); + FragmentTransaction ft = fm.beginTransaction(); + loading.show(ft, DIALOG_WAIT_TAG); + + } + + + /** + * Dismiss loading dialog + */ + public void dismissLoadingDialog(){ + Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG); + if (frag != null) { + LoadingDialog loading = (LoadingDialog) frag; + loading.dismiss(); } } diff --git a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java index 3c856734..d5c0d5a9 100644 --- a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -38,14 +38,12 @@ import android.content.res.Resources.NotFoundException; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; -import android.os.Handler; import android.os.IBinder; import android.preference.PreferenceManager; import android.provider.MediaStore; import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; -import android.support.v4.content.LocalBroadcastManager; +//import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import android.view.View; import android.view.ViewGroup; @@ -61,7 +59,6 @@ import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.Window; import com.owncloud.android.MainApp; import com.owncloud.android.R; -import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.services.FileDownloader; import com.owncloud.android.files.services.FileObserverService; @@ -70,11 +67,10 @@ import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder; import com.owncloud.android.files.services.FileUploader.FileUploaderBinder; import com.owncloud.android.operations.CreateFolderOperation; -import com.owncloud.android.lib.operations.common.OnRemoteOperationListener; import com.owncloud.android.lib.operations.common.RemoteOperation; import com.owncloud.android.lib.operations.common.RemoteOperationResult; import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode; - +import com.owncloud.android.operations.CreateShareOperation; import com.owncloud.android.operations.RemoveFileOperation; import com.owncloud.android.operations.RenameFileOperation; import com.owncloud.android.operations.SynchronizeFileOperation; @@ -83,7 +79,6 @@ import com.owncloud.android.services.OperationsService; import com.owncloud.android.syncadapter.FileSyncAdapter; import com.owncloud.android.ui.dialog.EditNameDialog; import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener; -import com.owncloud.android.ui.dialog.LoadingDialog; import com.owncloud.android.ui.dialog.SslValidatorDialog; import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener; import com.owncloud.android.ui.fragment.FileDetailFragment; @@ -103,18 +98,15 @@ import com.owncloud.android.utils.Log_OC; * @author David A. Velasco */ -public class FileDisplayActivity extends FileActivity implements -OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener, OnSslValidatorListener, OnRemoteOperationListener, EditNameDialogListener { +public class FileDisplayActivity extends HookActivity implements +OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener, OnSslValidatorListener, EditNameDialogListener { private ArrayAdapter mDirectories; - /** Access point to the cached database for the current ownCloud {@link Account} */ - private FileDataStorageManager mStorageManager = null; - private SyncBroadcastReceiver mSyncBroadcastReceiver; private UploadFinishReceiver mUploadFinishReceiver; private DownloadFinishReceiver mDownloadFinishReceiver; - private OperationsServiceReceiver mOperationsServiceReceiver; + //private OperationsServiceReceiver mOperationsServiceReceiver; private FileDownloaderBinder mDownloaderBinder = null; private FileUploaderBinder mUploaderBinder = null; private ServiceConnection mDownloadConnection = null, mUploadConnection = null; @@ -133,8 +125,6 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa private static final int DIALOG_SSL_VALIDATOR = 2; private static final int DIALOG_CERT_NOT_SAVED = 3; - private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT"; - public static final String ACTION_DETAILS = "com.owncloud.android.ui.activity.action.DETAILS"; private static final int ACTION_SELECT_CONTENT_FROM_APPS = 1; @@ -146,10 +136,9 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa private static final String TAG_SECOND_FRAGMENT = "SECOND_FRAGMENT"; private OCFile mWaitingToPreview; - private Handler mHandler; private boolean mSyncInProgress = false; - private boolean mRefreshSharesInProgress = false; + //private boolean mRefreshSharesInProgress = false; @Override protected void onCreate(Bundle savedInstanceState) { @@ -158,8 +147,6 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa super.onCreate(savedInstanceState); // this calls onAccountChanged() when ownCloud Account is valid - mHandler = new Handler(); - /// bindings to transference services mUploadConnection = new ListServiceConnection(); mDownloadConnection = new ListServiceConnection(); @@ -182,12 +169,12 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa if(savedInstanceState != null) { mWaitingToPreview = (OCFile) savedInstanceState.getParcelable(FileDisplayActivity.KEY_WAITING_TO_PREVIEW); mSyncInProgress = savedInstanceState.getBoolean(KEY_SYNC_IN_PROGRESS); - mRefreshSharesInProgress = savedInstanceState.getBoolean(KEY_REFRESH_SHARES_IN_PROGRESS); + //mRefreshSharesInProgress = savedInstanceState.getBoolean(KEY_REFRESH_SHARES_IN_PROGRESS); } else { mWaitingToPreview = null; mSyncInProgress = false; - mRefreshSharesInProgress = false; + //mRefreshSharesInProgress = false; } /// USER INTERFACE @@ -204,7 +191,7 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa // Action bar setup mDirectories = new CustomArrayAdapter(this, R.layout.sherlock_spinner_dropdown_item); getSupportActionBar().setHomeButtonEnabled(true); // mandatory since Android ICS, according to the official documentation - setSupportProgressBarIndeterminateVisibility(mSyncInProgress || mRefreshSharesInProgress); // always AFTER setContentView(...) ; to work around bug in its implementation + setSupportProgressBarIndeterminateVisibility(mSyncInProgress /*|| mRefreshSharesInProgress*/); // always AFTER setContentView(...) ; to work around bug in its implementation Log_OC.d(TAG, "onCreate() end"); } @@ -230,9 +217,8 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa */ @Override protected void onAccountSet(boolean stateWasRecovered) { + super.onAccountSet(stateWasRecovered); if (getAccount() != null) { - mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver()); - /// Check whether the 'main' OCFile handled by the Activity is contained in the current Account OCFile file = getFile(); // get parent from path @@ -242,15 +228,15 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa // upload in progress - right now, files are not inserted in the local cache until the upload is successful // get parent from path parentPath = file.getRemotePath().substring(0, file.getRemotePath().lastIndexOf(file.getFileName())); - if (mStorageManager.getFileByPath(parentPath) == null) + if (getStorageManager().getFileByPath(parentPath) == null) file = null; // not able to know the directory where the file is uploading } else { - file = mStorageManager.getFileByPath(file.getRemotePath()); // currentDir = null if not in the current Account + file = getStorageManager().getFileByPath(file.getRemotePath()); // currentDir = null if not in the current Account } } if (file == null) { // fall back to root folder - file = mStorageManager.getFileByPath(OCFile.ROOT_PATH); // never returns null + file = getStorageManager().getFileByPath(OCFile.ROOT_PATH); // never returns null } setFile(file); setNavigationListWithFolder(file); @@ -266,10 +252,6 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa updateFragmentsVisibility(!file.isFolder()); updateNavigationElementsInActionBar(file.isFolder() ? null : file); } - - - } else { - Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!"); } } @@ -282,10 +264,9 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa if (fileIt.isFolder()) { mDirectories.add(fileIt.getFileName()); } - //fileIt = mStorageManager.getFileById(fileIt.getParentId()); // get parent from path parentPath = fileIt.getRemotePath().substring(0, fileIt.getRemotePath().lastIndexOf(fileIt.getFileName())); - fileIt = mStorageManager.getFileByPath(parentPath); + fileIt = getStorageManager().getFileByPath(parentPath); } mDirectories.add(OCFile.PATH_SEPARATOR); } @@ -447,12 +428,12 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa boolean detailsFragmentChanged = false; if (waitedPreview) { if (success) { - mWaitingToPreview = mStorageManager.getFileById(mWaitingToPreview.getFileId()); // update the file from database, for the local storage path + mWaitingToPreview = getStorageManager().getFileById(mWaitingToPreview.getFileId()); // update the file from database, for the local storage path if (PreviewMediaFragment.canBePreviewed(mWaitingToPreview)) { startMediaPreview(mWaitingToPreview, 0, true); detailsFragmentChanged = true; } else { - openFile(mWaitingToPreview); + getFileOperationsHelper().openFile(mWaitingToPreview, this); } } mWaitingToPreview = null; @@ -464,7 +445,6 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa } } - @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getSherlock().getMenuInflater(); @@ -543,7 +523,7 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa targetPath = mDirectories.getItem(i) + OCFile.PATH_SEPARATOR + targetPath; } targetPath = OCFile.PATH_SEPARATOR + targetPath; - OCFile targetFolder = mStorageManager.getFileByPath(targetPath); + OCFile targetFolder = getStorageManager().getFileByPath(targetPath); if (targetFolder != null) { browseTo(targetFolder); } @@ -551,7 +531,8 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa // the next operation triggers a new call to this method, but it's necessary to // ensure that the name exposed in the action bar is the current directory when the // user selected it in the navigation list - getSupportActionBar().setSelectedNavigationItem(0); + if (getSupportActionBar().getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST && itemPosition != 0) + getSupportActionBar().setSelectedNavigationItem(0); } return true; } @@ -677,7 +658,7 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa super.onSaveInstanceState(outState); outState.putParcelable(FileDisplayActivity.KEY_WAITING_TO_PREVIEW, mWaitingToPreview); outState.putBoolean(FileDisplayActivity.KEY_SYNC_IN_PROGRESS, mSyncInProgress); - outState.putBoolean(FileDisplayActivity.KEY_REFRESH_SHARES_IN_PROGRESS, mRefreshSharesInProgress); + //outState.putBoolean(FileDisplayActivity.KEY_REFRESH_SHARES_IN_PROGRESS, mRefreshSharesInProgress); Log_OC.d(TAG, "onSaveInstanceState() end"); } @@ -692,12 +673,13 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa // Listen for sync messages IntentFilter syncIntentFilter = new IntentFilter(FileSyncAdapter.EVENT_FULL_SYNC_START); syncIntentFilter.addAction(FileSyncAdapter.EVENT_FULL_SYNC_END); - syncIntentFilter.addAction(FileSyncAdapter.EVENT_FOLDER_SIZE_SYNCED); - syncIntentFilter.addAction(FileSyncAdapter.EVENT_FOLDER_CONTENTS_SYNCED); - syncIntentFilter.addAction(SynchronizeFolderOperation.EVENT_SINGLE_FOLDER_SYNCED); + syncIntentFilter.addAction(FileSyncAdapter.EVENT_FULL_SYNC_FOLDER_SIZE_SYNCED); + syncIntentFilter.addAction(FileSyncAdapter.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED); + syncIntentFilter.addAction(SynchronizeFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED); + syncIntentFilter.addAction(SynchronizeFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED); mSyncBroadcastReceiver = new SyncBroadcastReceiver(); - //registerReceiver(mSyncBroadcastReceiver, syncIntentFilter); - LocalBroadcastManager.getInstance(this).registerReceiver(mSyncBroadcastReceiver, syncIntentFilter); + registerReceiver(mSyncBroadcastReceiver, syncIntentFilter); + //LocalBroadcastManager.getInstance(this).registerReceiver(mSyncBroadcastReceiver, syncIntentFilter); // Listen for upload messages IntentFilter uploadIntentFilter = new IntentFilter(FileUploader.getUploadFinishMessage()); @@ -711,10 +693,12 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa registerReceiver(mDownloadFinishReceiver, downloadIntentFilter); // Listen for messages from the OperationsService + /* IntentFilter operationsIntentFilter = new IntentFilter(OperationsService.ACTION_OPERATION_ADDED); operationsIntentFilter.addAction(OperationsService.ACTION_OPERATION_FINISHED); mOperationsServiceReceiver = new OperationsServiceReceiver(); LocalBroadcastManager.getInstance(this).registerReceiver(mOperationsServiceReceiver, operationsIntentFilter); + */ Log_OC.d(TAG, "onResume() end"); } @@ -725,8 +709,8 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa super.onPause(); Log_OC.e(TAG, "onPause() start"); if (mSyncBroadcastReceiver != null) { - //unregisterReceiver(mSyncBroadcastReceiver); - LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver); + unregisterReceiver(mSyncBroadcastReceiver); + //LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver); mSyncBroadcastReceiver = null; } if (mUploadFinishReceiver != null) { @@ -737,10 +721,12 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa unregisterReceiver(mDownloadFinishReceiver); mDownloadFinishReceiver = null; } + /* if (mOperationsServiceReceiver != null) { LocalBroadcastManager.getInstance(this).unregisterReceiver(mOperationsServiceReceiver); mOperationsServiceReceiver = null; } + */ Log_OC.d(TAG, "onPause() end"); } @@ -837,30 +823,6 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa /** - * Show loading dialog - */ - public void showLoadingDialog() { - // Construct dialog - LoadingDialog loading = new LoadingDialog(getResources().getString(R.string.wait_a_moment)); - FragmentManager fm = getSupportFragmentManager(); - FragmentTransaction ft = fm.beginTransaction(); - loading.show(ft, DIALOG_WAIT_TAG); - - } - - /** - * Dismiss loading dialog - */ - public void dismissLoadingDialog(){ - Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG); - if (frag != null) { - LoadingDialog loading = (LoadingDialog) frag; - loading.dismiss(); - } - } - - - /** * Translates a content URI of an image to a physical path * on the disk * @param uri The URI to resolve @@ -912,6 +874,8 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa ((TextView) v).setTextColor(getResources().getColorStateList( android.R.color.white)); + + fixRoot((TextView) v ); return v; } @@ -922,9 +886,16 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa ((TextView) v).setTextColor(getResources().getColorStateList( android.R.color.white)); + fixRoot((TextView) v ); return v; } + private void fixRoot(TextView v) { + if (v.getText().equals(OCFile.PATH_SEPARATOR)) { + v.setText(R.string.default_display_name_for_root_folder); + } + } + } private class SyncBroadcastReceiver extends BroadcastReceiver { @@ -935,17 +906,20 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa @Override public void onReceive(Context context, Intent intent) { String event = intent.getAction(); + Log_OC.d(TAG, "Received broadcast " + event); String accountName = intent.getStringExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME); String synchFolderRemotePath = intent.getStringExtra(FileSyncAdapter.EXTRA_FOLDER_PATH); RemoteOperationResult synchResult = (RemoteOperationResult)intent.getSerializableExtra(FileSyncAdapter.EXTRA_RESULT); - boolean sameAccount = (getAccount() != null && accountName.equals(getAccount().name) && mStorageManager != null); + boolean sameAccount = (getAccount() != null && accountName.equals(getAccount().name) && getStorageManager() != null); if (sameAccount) { - if (!FileSyncAdapter.EVENT_FULL_SYNC_START.equals(event)) { + if (FileSyncAdapter.EVENT_FULL_SYNC_START.equals(event)) { + mSyncInProgress = true; - OCFile currentFile = (getFile() == null) ? null : mStorageManager.getFileByPath(getFile().getRemotePath()); - OCFile currentDir = (getCurrentDir() == null) ? null : mStorageManager.getFileByPath(getCurrentDir().getRemotePath()); + } else { + OCFile currentFile = (getFile() == null) ? null : getStorageManager().getFileByPath(getFile().getRemotePath()); + OCFile currentDir = (getCurrentDir() == null) ? null : getStorageManager().getFileByPath(getCurrentDir().getRemotePath()); if (currentDir == null) { // current folder was removed from the server @@ -971,22 +945,25 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa setFile(currentFile); } - mSyncInProgress = (!FileSyncAdapter.EVENT_FULL_SYNC_END.equals(event) && - !SynchronizeFolderOperation.EVENT_SINGLE_FOLDER_SYNCED.equals(event) && - (synchResult == null || synchResult.isSuccess())) ; - + mSyncInProgress = (!FileSyncAdapter.EVENT_FULL_SYNC_END.equals(event) && !SynchronizeFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED.equals(event)); + + /* if (synchResult != null && synchResult.isSuccess() && (SynchronizeFolderOperation.EVENT_SINGLE_FOLDER_SYNCED.equals(event) || - FileSyncAdapter.EVENT_FOLDER_CONTENTS_SYNCED.equals(event) - ) + FileSyncAdapter.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED.equals(event) + ) && + !mRefreshSharesInProgress && + getFileOperationsHelper().isSharedSupported(FileDisplayActivity.this) ) { startGetShares(); } + */ } - //removeStickyBroadcast(intent); - setSupportProgressBarIndeterminateVisibility(mSyncInProgress || mRefreshSharesInProgress); + removeStickyBroadcast(intent); + Log_OC.d(TAG, "Setting progress visibility to " + mSyncInProgress); + setSupportProgressBarIndeterminateVisibility(mSyncInProgress /*|| mRefreshSharesInProgress*/); } if (synchResult != null) { @@ -1069,12 +1046,12 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa if (OperationsService.ACTION_OPERATION_ADDED.equals(intent.getAction())) { } else if (OperationsService.ACTION_OPERATION_FINISHED.equals(intent.getAction())) { - mRefreshSharesInProgress = false; + //mRefreshSharesInProgress = false; Account account = intent.getParcelableExtra(OperationsService.EXTRA_ACCOUNT); RemoteOperationResult getSharesResult = (RemoteOperationResult)intent.getSerializableExtra(OperationsService.EXTRA_RESULT); if (getAccount() != null && account.name.equals(getAccount().name) - && mStorageManager != null + && getStorageManager() != null ) { refeshListOfFilesFragment(); } @@ -1084,30 +1061,20 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa showDialog(DIALOG_SSL_VALIDATOR); } - setSupportProgressBarIndeterminateVisibility(mRefreshSharesInProgress || mSyncInProgress); + //setSupportProgressBarIndeterminateVisibility(mRefreshSharesInProgress || mSyncInProgress); } } } - - /** - * {@inheritDoc} - */ - @Override - public FileDataStorageManager getStorageManager() { - return mStorageManager; - } - - public void browseToRoot() { OCFileListFragment listOfFiles = getListOfFilesFragment(); if (listOfFiles != null) { // should never be null, indeed while (mDirectories.getCount() > 1) { popDirname(); } - OCFile root = mStorageManager.getFileByPath(OCFile.ROOT_PATH); + OCFile root = getStorageManager().getFileByPath(OCFile.ROOT_PATH); listOfFiles.listDirectory(root); setFile(listOfFiles.getCurrentFile()); startSyncFolderOperation(root); @@ -1220,9 +1187,13 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa if (chosenFile == null || mDualPane) { // only list of files - set for browsing through folders OCFile currentDir = getCurrentDir(); - actionBar.setDisplayHomeAsUpEnabled(currentDir != null && currentDir.getParentId() != 0); - actionBar.setDisplayShowTitleEnabled(false); - actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); + boolean noRoot = (currentDir != null && currentDir.getParentId() != 0); + actionBar.setDisplayHomeAsUpEnabled(noRoot); + actionBar.setDisplayShowTitleEnabled(!noRoot); + if (!noRoot) { + actionBar.setTitle(getString(R.string.default_display_name_for_root_folder)); + } + actionBar.setNavigationMode(!noRoot ? ActionBar.NAVIGATION_MODE_STANDARD : ActionBar.NAVIGATION_MODE_LIST); actionBar.setListNavigationCallbacks(mDirectories, this); // assuming mDirectories is updated } else { @@ -1343,6 +1314,8 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa */ @Override public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) { + super.onRemoteOperationFinish(operation, result); + if (operation instanceof RemoveFileOperation) { onRemoveFileOperationFinish((RemoveFileOperation)operation, result); @@ -1354,10 +1327,21 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa } else if (operation instanceof CreateFolderOperation) { onCreateFolderOperationFinish((CreateFolderOperation)operation, result); + + } else if (operation instanceof CreateShareOperation) { + onCreateShareOperationFinish((CreateShareOperation) operation, result); } + } + private void onCreateShareOperationFinish(CreateShareOperation operation, RemoteOperationResult result) { + if (result.isSuccess()) { + refeshListOfFilesFragment(); + } + } + + /** * Updates the view associated to the activity after the finish of an operation trying to remove a * file. @@ -1376,7 +1360,7 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa if (second != null && removedFile.equals(second.getFile())) { cleanSecondFragment(); } - if (mStorageManager.getFileById(removedFile.getParentId()).equals(getCurrentDir())) { + if (getStorageManager().getFileById(removedFile.getParentId()).equals(getCurrentDir())) { refeshListOfFilesFragment(); } @@ -1435,7 +1419,7 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa ((FileDetailFragment) details).updateFileDetails(renamedFile, getAccount()); } } - if (mStorageManager.getFileById(renamedFile.getParentId()).equals(getCurrentDir())) { + if (getStorageManager().getFileById(renamedFile.getParentId()).equals(getCurrentDir())) { refeshListOfFilesFragment(); } @@ -1511,11 +1495,11 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa // Create directory path += newDirectoryName + OCFile.PATH_SEPARATOR; - RemoteOperation operation = new CreateFolderOperation(path, false, mStorageManager); + RemoteOperation operation = new CreateFolderOperation(path, false, getStorageManager()); operation.execute( getAccount(), FileDisplayActivity.this, FileDisplayActivity.this, - mHandler, + getHandler(), FileDisplayActivity.this); showLoadingDialog(); @@ -1540,9 +1524,9 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa if (file != null) { if (file.isFolder()) { return file; - } else if (mStorageManager != null) { + } else if (getStorageManager() != null) { String parentPath = file.getRemotePath().substring(0, file.getRemotePath().lastIndexOf(file.getFileName())); - return mStorageManager.getFileByPath(parentPath); + return getStorageManager().getFileByPath(parentPath); } } return null; @@ -1557,6 +1541,7 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa RemoteOperation synchFolderOp = new SynchronizeFolderOperation( folder, currentSyncTime, false, + getFileOperationsHelper().isSharedSupported(this), getStorageManager(), getAccount(), getApplicationContext() @@ -1566,7 +1551,7 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa setSupportProgressBarIndeterminateVisibility(true); } - + /* private void startGetShares() { // Get shared files/folders Intent intent = new Intent(this, OperationsService.class); @@ -1575,5 +1560,6 @@ OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNa mRefreshSharesInProgress = true; } - + */ + } diff --git a/src/com/owncloud/android/ui/activity/HookActivity.java b/src/com/owncloud/android/ui/activity/HookActivity.java new file mode 100644 index 00000000..54d65b1b --- /dev/null +++ b/src/com/owncloud/android/ui/activity/HookActivity.java @@ -0,0 +1,24 @@ +/* ownCloud Android client application + * Copyright (C) 2012-2014 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.ui.activity; + +public abstract class HookActivity extends FileActivity { + + private static final String TAG = HookActivity.class.getName(); + +} diff --git a/src/com/owncloud/android/ui/activity/Preferences.java b/src/com/owncloud/android/ui/activity/Preferences.java index 05a631d0..3ebb432c 100644 --- a/src/com/owncloud/android/ui/activity/Preferences.java +++ b/src/com/owncloud/android/ui/activity/Preferences.java @@ -17,6 +17,7 @@ */ package com.owncloud.android.ui.activity; +import android.accounts.Account; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageInfo; @@ -35,6 +36,7 @@ import com.actionbarsherlock.app.SherlockPreferenceActivity; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; import com.owncloud.android.R; +import com.owncloud.android.authentication.AccountUtils; import com.owncloud.android.db.DbHandler; import com.owncloud.android.utils.DisplayUtils; import com.owncloud.android.utils.Log_OC; @@ -131,18 +133,19 @@ public class Preferences extends SherlockPreferenceActivity { Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setType("text/plain"); - //Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this); + intent.setData(Uri.parse(getString(R.string.mail_recommend))); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + String appName = getString(R.string.app_name); - //String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@')); - //String recommendSubject = String.format(getString(R.string.recommend_subject), username, appName); + String downloadUrl = getString(R.string.url_app_download); + Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(Preferences.this); + String username = currentAccount.name.substring(0, currentAccount.name.lastIndexOf('@')); + String recommendSubject = String.format(getString(R.string.recommend_subject), appName); + String recommendText = String.format(getString(R.string.recommend_text), appName, downloadUrl, username); + intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject); - //String recommendText = String.format(getString(R.string.recommend_text), getString(R.string.app_name), username); - String recommendText = String.format(getString(R.string.recommend_text), getString(R.string.app_name), getString(R.string.url_app_download)); intent.putExtra(Intent.EXTRA_TEXT, recommendText); - - intent.setData(Uri.parse(getString(R.string.mail_recommend))); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); diff --git a/src/com/owncloud/android/ui/activity/UploadFilesActivity.java b/src/com/owncloud/android/ui/activity/UploadFilesActivity.java index 7eff9b45..8b4a41d2 100644 --- a/src/com/owncloud/android/ui/activity/UploadFilesActivity.java +++ b/src/com/owncloud/android/ui/activity/UploadFilesActivity.java @@ -380,6 +380,7 @@ public class UploadFilesActivity extends FileActivity implements @Override protected void onAccountSet(boolean stateWasRecovered) { + super.onAccountSet(stateWasRecovered); if (getAccount() != null) { if (!mAccountOnCreation.equals(getAccount())) { setResult(RESULT_CANCELED); @@ -387,7 +388,6 @@ public class UploadFilesActivity extends FileActivity implements } } else { - Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!"); setResult(RESULT_CANCELED); finish(); } diff --git a/src/com/owncloud/android/ui/dialog/ActivityChooserDialog.java b/src/com/owncloud/android/ui/dialog/ActivityChooserDialog.java new file mode 100644 index 00000000..dc15f69a --- /dev/null +++ b/src/com/owncloud/android/ui/dialog/ActivityChooserDialog.java @@ -0,0 +1,151 @@ +/* ownCloud Android client application + * Copyright (C) 2012-2014 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.ui.dialog; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import android.app.AlertDialog; +import android.app.Dialog; +import android.content.ComponentName; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.pm.ActivityInfo; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; + +import com.actionbarsherlock.app.SherlockDialogFragment; +import com.owncloud.android.R; +import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.files.FileOperationsHelper; +import com.owncloud.android.ui.activity.FileActivity; +import com.owncloud.android.utils.Log_OC; + +/** + * Dialog showing a list activities able to resolve a given Intent, + * filtering out the activities matching give package names. + * + * @author David A. Velasco + */ +public class ActivityChooserDialog extends SherlockDialogFragment { + + private final static String TAG = ActivityChooserDialog.class.getSimpleName(); + private final static String ARG_INTENT = ActivityChooserDialog.class.getSimpleName() + ".ARG_INTENT"; + private final static String ARG_PACKAGES_TO_EXCLUDE = ActivityChooserDialog.class.getSimpleName() + ".ARG_PACKAGES_TO_EXCLUDE"; + private final static String ARG_FILE_TO_SHARE = ActivityChooserDialog.class.getSimpleName() + ".FILE_TO_SHARE"; + + private ActivityAdapter mAdapter; + private OCFile mFile; + private Intent mIntent; + + public static ActivityChooserDialog newInstance(Intent intent, String[] packagesToExclude, OCFile fileToShare) { + ActivityChooserDialog f = new ActivityChooserDialog(); + Bundle args = new Bundle(); + args.putParcelable(ARG_INTENT, intent); + args.putStringArray(ARG_PACKAGES_TO_EXCLUDE, packagesToExclude); + args.putParcelable(ARG_FILE_TO_SHARE, fileToShare); + f.setArguments(args); + return f; + } + + public ActivityChooserDialog() { + super(); + Log_OC.d(TAG, "constructor"); + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + mIntent = getArguments().getParcelable(ARG_INTENT); + String[] packagesToExclude = getArguments().getStringArray(ARG_PACKAGES_TO_EXCLUDE); + List packagesToExcludeList = Arrays.asList(packagesToExclude != null ? packagesToExclude : new String[0]); + mFile = getArguments().getParcelable(ARG_FILE_TO_SHARE); + + PackageManager pm= getSherlockActivity().getPackageManager(); + List activities = pm.queryIntentActivities(mIntent, PackageManager.MATCH_DEFAULT_ONLY); + Iterator it = activities.iterator(); + ResolveInfo resolveInfo; + while (it.hasNext()) { + resolveInfo = it.next(); + if (packagesToExcludeList.contains(resolveInfo.activityInfo.packageName.toLowerCase())) { + it.remove(); + } + } + Collections.sort(activities, new ResolveInfo.DisplayNameComparator(pm)); + mAdapter = new ActivityAdapter(getSherlockActivity(), pm, activities); + + return new AlertDialog.Builder(getSherlockActivity()) + .setTitle(R.string.activity_chooser_title) + .setAdapter(mAdapter, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + // Add the information of the chosen activity to the intent to send + ResolveInfo chosen = mAdapter.getItem(which); + ActivityInfo actInfo = chosen.activityInfo; + ComponentName name=new ComponentName(actInfo.applicationInfo.packageName, actInfo.name); + mIntent.setComponent(name); + + // Create a new share resource + FileOperationsHelper foh = new FileOperationsHelper(); + foh.shareFileWithLinkToApp(mFile, mIntent, (FileActivity)getSherlockActivity()); + } + }) + .create(); + } + + + class ActivityAdapter extends ArrayAdapter { + + private PackageManager mPackageManager; + + ActivityAdapter(Context context, PackageManager pm, List apps) { + super(context, R.layout.activity_row, apps); + this.mPackageManager = pm; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + if (convertView == null) { + convertView = newView(parent); + } + bindView(position, convertView); + return convertView; + } + + private View newView(ViewGroup parent) { + return(((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.activity_row, parent, false)); + } + + private void bindView(int position, View row) { + TextView label = (TextView) row.findViewById(R.id.title); + label.setText(getItem(position).loadLabel(mPackageManager)); + ImageView icon = (ImageView) row.findViewById(R.id.icon); + icon.setImageDrawable(getItem(position).loadIcon(mPackageManager)); + } + } + +} diff --git a/src/com/owncloud/android/ui/fragment/FileDetailFragment.java b/src/com/owncloud/android/ui/fragment/FileDetailFragment.java index 49895260..216cceec 100644 --- a/src/com/owncloud/android/ui/fragment/FileDetailFragment.java +++ b/src/com/owncloud/android/ui/fragment/FileDetailFragment.java @@ -335,8 +335,14 @@ public class FileDetailFragment extends FileFragment implements @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { + case R.id.action_share_file: { + FileDisplayActivity activity = (FileDisplayActivity) getSherlockActivity(); + activity.getFileOperationsHelper().shareFileWithLink(getFile(), activity); + return true; + } case R.id.action_open_file_with: { - mContainerActivity.openFile(getFile()); + FileDisplayActivity activity = (FileDisplayActivity) getSherlockActivity(); + activity.getFileOperationsHelper().openFile(getFile(), activity); return true; } case R.id.action_remove_file: { @@ -358,7 +364,7 @@ public class FileDetailFragment extends FileFragment implements return false; } } - + @Override public void onClick(View v) { switch (v.getId()) { @@ -399,7 +405,6 @@ public class FileDetailFragment extends FileFragment implements } } - private void removeFile() { OCFile file = getFile(); ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance( diff --git a/src/com/owncloud/android/ui/fragment/FileFragment.java b/src/com/owncloud/android/ui/fragment/FileFragment.java index b6c65749..2f1a49b1 100644 --- a/src/com/owncloud/android/ui/fragment/FileFragment.java +++ b/src/com/owncloud/android/ui/fragment/FileFragment.java @@ -21,7 +21,6 @@ import android.support.v4.app.Fragment; import com.actionbarsherlock.app.SherlockFragment; import com.owncloud.android.datamodel.OCFile; -import com.owncloud.android.files.FileHandler; import com.owncloud.android.ui.activity.TransferServiceGetter; @@ -73,7 +72,7 @@ public class FileFragment extends SherlockFragment { * * @author David A. Velasco */ - public interface ContainerActivity extends TransferServiceGetter, FileHandler { + public interface ContainerActivity extends TransferServiceGetter { /** * Callback method invoked when the detail fragment wants to notice its container @@ -95,8 +94,7 @@ public class FileFragment extends SherlockFragment { * @param file File to show details */ public void showDetails(OCFile file); - - + } } diff --git a/src/com/owncloud/android/ui/fragment/OCFileListFragment.java b/src/com/owncloud/android/ui/fragment/OCFileListFragment.java index 91abe7d1..96aa2dc2 100644 --- a/src/com/owncloud/android/ui/fragment/OCFileListFragment.java +++ b/src/com/owncloud/android/ui/fragment/OCFileListFragment.java @@ -25,7 +25,6 @@ import com.owncloud.android.R; import com.owncloud.android.authentication.AccountUtils; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; -import com.owncloud.android.files.FileHandler; import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder; import com.owncloud.android.files.services.FileUploader.FileUploaderBinder; import com.owncloud.android.lib.operations.common.OnRemoteOperationListener; @@ -183,8 +182,8 @@ public class OCFileListFragment extends ExtendedListFragment implements EditName // media preview mContainerActivity.startMediaPreview(file, 0, true); } else { - // open with - mContainerActivity.openFile(file); + FileDisplayActivity activity = (FileDisplayActivity) getSherlockActivity(); + activity.getFileOperationsHelper().openFile(file, activity); } } else { @@ -284,6 +283,11 @@ public class OCFileListFragment extends ExtendedListFragment implements EditName AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); mTargetFile = (OCFile) mAdapter.getItem(info.position); switch (item.getItemId()) { + case R.id.action_share_file: { + FileDisplayActivity activity = (FileDisplayActivity) getSherlockActivity(); + activity.getFileOperationsHelper().shareFileWithLink(mTargetFile, activity); + return true; + } case R.id.action_rename_file: { String fileName = mTargetFile.getFileName(); int extensionStart = mTargetFile.isFolder() ? -1 : fileName.lastIndexOf("."); @@ -410,7 +414,7 @@ public class OCFileListFragment extends ExtendedListFragment implements EditName * * @author David A. Velasco */ - public interface ContainerActivity extends TransferServiceGetter, OnRemoteOperationListener, FileHandler { + public interface ContainerActivity extends TransferServiceGetter, OnRemoteOperationListener { /** * Callback method invoked when a the user browsed into a different folder through the list of files diff --git a/src/com/owncloud/android/ui/preview/PreviewImageActivity.java b/src/com/owncloud/android/ui/preview/PreviewImageActivity.java index edafa65d..374a53b7 100644 --- a/src/com/owncloud/android/ui/preview/PreviewImageActivity.java +++ b/src/com/owncloud/android/ui/preview/PreviewImageActivity.java @@ -70,8 +70,6 @@ public class PreviewImageActivity extends FileActivity implements FileFragment.C private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT"; - private FileDataStorageManager mStorageManager; - private ViewPager mViewPager; private PreviewImagePagerAdapter mPreviewImagePagerAdapter; @@ -115,13 +113,12 @@ public class PreviewImageActivity extends FileActivity implements FileFragment.C private void initViewPager() { // get parent from path String parentPath = getFile().getRemotePath().substring(0, getFile().getRemotePath().lastIndexOf(getFile().getFileName())); - OCFile parentFolder = mStorageManager.getFileByPath(parentPath); - //OCFile parentFolder = mStorageManager.getFileById(getFile().getParentId()); + OCFile parentFolder = getStorageManager().getFileByPath(parentPath); if (parentFolder == null) { // should not be necessary - parentFolder = mStorageManager.getFileByPath(OCFile.ROOT_PATH); + parentFolder = getStorageManager().getFileByPath(OCFile.ROOT_PATH); } - mPreviewImagePagerAdapter = new PreviewImagePagerAdapter(getSupportFragmentManager(), parentFolder, getAccount(), mStorageManager); + mPreviewImagePagerAdapter = new PreviewImagePagerAdapter(getSupportFragmentManager(), parentFolder, getAccount(), getStorageManager()); mViewPager = (ViewPager) findViewById(R.id.fragmentPager); int position = mPreviewImagePagerAdapter.getFilePosition(getFile()); position = (position >= 0) ? position : 0; @@ -385,7 +382,7 @@ public class PreviewImageActivity extends FileActivity implements FileFragment.C if (getAccount().name.equals(accountName) && downloadedRemotePath != null) { - OCFile file = mStorageManager.getFileByPath(downloadedRemotePath); + OCFile file = getStorageManager().getFileByPath(downloadedRemotePath); int position = mPreviewImagePagerAdapter.getFilePosition(file); boolean downloadWasFine = intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false); //boolean isOffscreen = Math.abs((mViewPager.getCurrentItem() - position)) <= mViewPager.getOffscreenPageLimit(); @@ -433,6 +430,7 @@ public class PreviewImageActivity extends FileActivity implements FileFragment.C @Override protected void onAccountSet(boolean stateWasRecovered) { + super.onAccountSet(stateWasRecovered); if (getAccount() != null) { OCFile file = getFile(); /// Validate handled file (first image to preview) @@ -442,15 +440,14 @@ public class PreviewImageActivity extends FileActivity implements FileFragment.C if (!file.isImage()) { throw new IllegalArgumentException("Non-image file passed as argument"); } - mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver()); // Update file according to DB file, if it is possible if (file.getFileId() > FileDataStorageManager.ROOT_PARENT_ID) - file = mStorageManager.getFileById(file.getFileId()); + file = getStorageManager().getFileById(file.getFileId()); if (file != null) { /// Refresh the activity according to the Account and OCFile set - setFile(file); // reset after getting it fresh from mStorageManager + setFile(file); // reset after getting it fresh from storageManager getSupportActionBar().setTitle(getFile().getFileName()); //if (!stateWasRecovered) { initViewPager(); @@ -460,9 +457,6 @@ public class PreviewImageActivity extends FileActivity implements FileFragment.C // handled file not in the current Account finish(); } - - } else { - Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!"); } } @@ -480,5 +474,5 @@ public class PreviewImageActivity extends FileActivity implements FileFragment.C startActivity(i); } } - + } diff --git a/src/com/owncloud/android/ui/preview/PreviewImageFragment.java b/src/com/owncloud/android/ui/preview/PreviewImageFragment.java index 8555fa77..a2d05370 100644 --- a/src/com/owncloud/android/ui/preview/PreviewImageFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewImageFragment.java @@ -57,6 +57,7 @@ import com.owncloud.android.lib.operations.common.OnRemoteOperationListener; import com.owncloud.android.lib.operations.common.RemoteOperation; import com.owncloud.android.lib.operations.common.RemoteOperationResult; import com.owncloud.android.operations.RemoveFileOperation; +import com.owncloud.android.ui.activity.FileActivity; import com.owncloud.android.ui.fragment.ConfirmationDialogFragment; import com.owncloud.android.ui.fragment.FileFragment; import com.owncloud.android.utils.Log_OC; @@ -247,6 +248,11 @@ public class PreviewImageFragment extends FileFragment implements OnRemoteOper @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { + case R.id.action_share_file: { + FileActivity act = (FileActivity)getSherlockActivity(); + act.getFileOperationsHelper().shareFileWithLink(getFile(), act); + return true; + } case R.id.action_open_file_with: { openFile(); return true; diff --git a/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java b/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java index 464ea6db..ee4a7b00 100644 --- a/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java @@ -307,6 +307,10 @@ public class PreviewMediaFragment extends FileFragment implements @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { + case R.id.action_share_file: { + shareFileWithLink(); + return true; + } case R.id.action_open_file_with: { openFile(); return true; @@ -326,6 +330,14 @@ public class PreviewMediaFragment extends FileFragment implements } + private void shareFileWithLink() { + stopPreview(false); + FileActivity activity = (FileActivity)((FileFragment.ContainerActivity)getActivity()); + activity.getFileOperationsHelper().shareFileWithLink(getFile(), activity); + + } + + private void seeDetails() { stopPreview(false); ((FileFragment.ContainerActivity)getActivity()).showDetails(getFile()); diff --git a/src/com/owncloud/android/ui/preview/PreviewVideoActivity.java b/src/com/owncloud/android/ui/preview/PreviewVideoActivity.java index 9e6a2803..5e5999b4 100644 --- a/src/com/owncloud/android/ui/preview/PreviewVideoActivity.java +++ b/src/com/owncloud/android/ui/preview/PreviewVideoActivity.java @@ -18,7 +18,6 @@ package com.owncloud.android.ui.preview; import com.owncloud.android.R; -import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.media.MediaService; import com.owncloud.android.ui.activity.FileActivity; @@ -60,8 +59,6 @@ public class PreviewVideoActivity extends FileActivity implements OnCompletionLi private static final String TAG = PreviewVideoActivity.class.getSimpleName(); - private FileDataStorageManager mStorageManager; - private int mSavedPlaybackPosition; // in the unit time handled by MediaPlayer.getCurrentPosition() private boolean mAutoplay; // when 'true', the playback starts immediately with the activity private VideoView mVideoPlayer; // view to play the file; both performs and show the playback @@ -194,6 +191,7 @@ public class PreviewVideoActivity extends FileActivity implements OnCompletionLi @Override protected void onAccountSet(boolean stateWasRecovered) { + super.onAccountSet(stateWasRecovered); if (getAccount() != null) { OCFile file = getFile(); /// Validate handled file (first image to preview) @@ -203,8 +201,7 @@ public class PreviewVideoActivity extends FileActivity implements OnCompletionLi if (!file.isVideo()) { throw new IllegalArgumentException("Non-video file passed as argument"); } - mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver()); - file = mStorageManager.getFileById(file.getFileId()); + file = getStorageManager().getFileById(file.getFileId()); if (file != null) { if (file.isDown()) { mVideoPlayer.setVideoPath(file.getStoragePath()); @@ -230,7 +227,6 @@ public class PreviewVideoActivity extends FileActivity implements OnCompletionLi finish(); } } else { - Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!"); finish(); } }