From 261299cf23c150b7f9086289c960b967e5ae54d7 Mon Sep 17 00:00:00 2001 From: "David A. Velasco" Date: Tue, 3 Nov 2015 16:55:34 +0100 Subject: [PATCH] Share public link in ShareFileFragment --- owncloud-android-library | 2 +- .../android/files/FileOperationsHelper.java | 41 +++++++++++++- .../CreateShareViaLinkOperation.java | 4 +- .../android/ui/activity/FileActivity.java | 5 +- .../ui/fragment/FileDetailFragment.java | 2 +- .../ui/fragment/OCFileListFragment.java | 2 +- .../ui/fragment/ShareFileFragment.java | 54 ++++++++++++------- .../ui/preview/PreviewImageFragment.java | 2 +- .../ui/preview/PreviewMediaFragment.java | 2 +- .../ui/preview/PreviewTextFragment.java | 2 +- 10 files changed, 88 insertions(+), 28 deletions(-) diff --git a/owncloud-android-library b/owncloud-android-library index b09969d078..c52f9c9b14 160000 --- a/owncloud-android-library +++ b/owncloud-android-library @@ -1 +1 @@ -Subproject commit b09969d078b3a790b01c8d61a7298a37439a9f24 +Subproject commit c52f9c9b149f6427eeebbb68ef670b83cf3487b4 diff --git a/src/com/owncloud/android/files/FileOperationsHelper.java b/src/com/owncloud/android/files/FileOperationsHelper.java index a22de8ef00..80f59643ae 100644 --- a/src/com/owncloud/android/files/FileOperationsHelper.java +++ b/src/com/owncloud/android/files/FileOperationsHelper.java @@ -139,7 +139,43 @@ public class FileOperationsHelper { .show(); } - public void shareFileWithLink(OCFile file) { + + /** + /** + * Helper method to share a file via a public link. Starts a request to do it in {@link OperationsService} + * + * @param file The file to share. + */ + public void shareFileViaLink(OCFile file) { + if (isSharedSupported()) { + if (file != null) { + mFileActivity.showLoadingDialog( + mFileActivity.getApplicationContext(). + getString(R.string.wait_a_moment) + ); + Intent service = new Intent(mFileActivity, OperationsService.class); + service.setAction(OperationsService.ACTION_CREATE_SHARE_VIA_LINK); + service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount()); + service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath()); + mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service); + + } else { + Log_OC.wtf(TAG, "Trying to share a NULL OCFile"); + // TODO user-level error? + } + + } else { + // Show a Message + Toast t = Toast.makeText( + mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api), + Toast.LENGTH_LONG + ); + t.show(); + } + } + + + public void shareFileWithLinkOLD(OCFile file) { if (isSharedSupported()) { if (file != null) { @@ -194,7 +230,7 @@ public class FileOperationsHelper { /** - * Helper method to share a file with a know sharee. Starts a request to do it in {@link OperationsService} + * Helper method to share a file with a known sharee. Starts a request to do it in {@link OperationsService} * * @param file The file to share. * @param shareeName Name (user name or group name) of the target sharee. @@ -492,4 +528,5 @@ public class FileOperationsHelper { } return false; } + } diff --git a/src/com/owncloud/android/operations/CreateShareViaLinkOperation.java b/src/com/owncloud/android/operations/CreateShareViaLinkOperation.java index 373c57d1dd..5958c7f1d8 100644 --- a/src/com/owncloud/android/operations/CreateShareViaLinkOperation.java +++ b/src/com/owncloud/android/operations/CreateShareViaLinkOperation.java @@ -148,10 +148,12 @@ public class CreateShareViaLinkOperation extends SyncOperation { // 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.setShareViaLink(true); getStorageManager().saveFile(file); + if (mSendIntent != null) { + mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink()); + } } } diff --git a/src/com/owncloud/android/ui/activity/FileActivity.java b/src/com/owncloud/android/ui/activity/FileActivity.java index 249ae8dfd9..6091c6f72e 100644 --- a/src/com/owncloud/android/ui/activity/FileActivity.java +++ b/src/com/owncloud/android/ui/activity/FileActivity.java @@ -785,7 +785,10 @@ public class FileActivity extends AppCompatActivity updateFileFromDB(); Intent sendIntent = operation.getSendIntentWithSubject(this); - startActivity(sendIntent); + if (sendIntent != null) { + startActivity(sendIntent); + } + } else { // Detect Failure (403) --> needs Password if (result.getCode() == ResultCode.SHARE_FORBIDDEN) { diff --git a/src/com/owncloud/android/ui/fragment/FileDetailFragment.java b/src/com/owncloud/android/ui/fragment/FileDetailFragment.java index 543378f599..4b07b3bc8b 100644 --- a/src/com/owncloud/android/ui/fragment/FileDetailFragment.java +++ b/src/com/owncloud/android/ui/fragment/FileDetailFragment.java @@ -227,7 +227,7 @@ public class FileDetailFragment extends FileFragment implements OnClickListener public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_share_file: { - mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile()); + mContainerActivity.getFileOperationsHelper().shareFileWithLinkOLD(getFile()); return true; } case R.id.action_share_with_users: { diff --git a/src/com/owncloud/android/ui/fragment/OCFileListFragment.java b/src/com/owncloud/android/ui/fragment/OCFileListFragment.java index 066ff0a272..f195f06eaf 100644 --- a/src/com/owncloud/android/ui/fragment/OCFileListFragment.java +++ b/src/com/owncloud/android/ui/fragment/OCFileListFragment.java @@ -344,7 +344,7 @@ public class OCFileListFragment extends ExtendedListFragment mTargetFile = (OCFile) mAdapter.getItem(filePosition); switch (menuId) { case R.id.action_share_file: { - mContainerActivity.getFileOperationsHelper().shareFileWithLink(mTargetFile); + mContainerActivity.getFileOperationsHelper().shareFileWithLinkOLD(mTargetFile); return true; } case R.id.action_share_with_users: { diff --git a/src/com/owncloud/android/ui/fragment/ShareFileFragment.java b/src/com/owncloud/android/ui/fragment/ShareFileFragment.java index ee979c60f2..16994de863 100644 --- a/src/com/owncloud/android/ui/fragment/ShareFileFragment.java +++ b/src/com/owncloud/android/ui/fragment/ShareFileFragment.java @@ -92,6 +92,9 @@ public class ShareFileFragment extends Fragment /** Public share bound to the file */ private OCShare mPublicShare; + /** Listener for changes on switch to share / unshare publicly */ + private CompoundButton.OnCheckedChangeListener mOnShareViaLinkSwitchCheckedChangeListener; + /** * Public factory method to create new ShareFileFragment instances. @@ -175,26 +178,26 @@ public class ShareFileFragment extends Fragment }); // Switch to create public share - Switch shareViaLinkSwitch = (Switch) view.findViewById(R.id.shareViaLinkSectionSwitch); - shareViaLinkSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + mOnShareViaLinkSwitchCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - if (isChecked) { - // TODO real implementation: create public share - // expand section - getExpirationDateSection().setVisibility(View.VISIBLE); - getPasswordSection().setVisibility(View.VISIBLE); - getGetLinkButton().setVisibility(View.VISIBLE); + if (isResumed()) { + if (isChecked) { + ((FileActivity) getActivity()).getFileOperationsHelper().shareFileViaLink(mFile); - } else { - // TODO real implementation: unshare - // collapse section - getExpirationDateSection().setVisibility(View.GONE); - getPasswordSection().setVisibility(View.GONE); - getGetLinkButton().setVisibility(View.GONE); - } + } else { + // TODO real implementation: unshare + // collapse section + getExpirationDateSection().setVisibility(View.GONE); + getPasswordSection().setVisibility(View.GONE); + getGetLinkButton().setVisibility(View.GONE); + } + } // else, nothing; very important, setCheched(...) is called automatically during Fragment + // recreation on device rotations } - }); + }; + Switch shareViaLinkSwitch = (Switch) view.findViewById(R.id.shareViaLinkSectionSwitch); + shareViaLinkSwitch.setOnCheckedChangeListener(mOnShareViaLinkSwitchCheckedChangeListener); // Switch for expiration date Switch shareViaLinkExpirationSwitch = (Switch) view.findViewById(R.id.shareViaLinkExpirationSwitch); @@ -344,14 +347,29 @@ public class ShareFileFragment extends Fragment private void updatePublicShareSection() { if (mPublicShare != null && ShareType.PUBLIC_LINK.equals(mPublicShare.getShareType())) { // public share bound -> expand section - getShareViaLinkSwitch().setChecked(true); + Switch shareViaLinkSwitch = getShareViaLinkSwitch(); + if (!shareViaLinkSwitch.isChecked()) { + // set null listener before setChecked() to prevent infinite loop of calls + shareViaLinkSwitch.setOnCheckedChangeListener(null); + getShareViaLinkSwitch().setChecked(true); + shareViaLinkSwitch.setOnCheckedChangeListener( + mOnShareViaLinkSwitchCheckedChangeListener + ); + } getExpirationDateSection().setVisibility(View.VISIBLE); getPasswordSection().setVisibility(View.VISIBLE); getGetLinkButton().setVisibility(View.VISIBLE); } else { // no public share -> collapse section - getShareViaLinkSwitch().setChecked(false); + Switch shareViaLinkSwitch = getShareViaLinkSwitch(); + if (shareViaLinkSwitch.isChecked()) { + shareViaLinkSwitch.setOnCheckedChangeListener(null); + getShareViaLinkSwitch().setChecked(false); + shareViaLinkSwitch.setOnCheckedChangeListener( + mOnShareViaLinkSwitchCheckedChangeListener + ); + } getExpirationDateSection().setVisibility(View.GONE); getPasswordSection().setVisibility(View.GONE); getGetLinkButton().setVisibility(View.GONE); diff --git a/src/com/owncloud/android/ui/preview/PreviewImageFragment.java b/src/com/owncloud/android/ui/preview/PreviewImageFragment.java index a114fabd60..ec1e3daa56 100644 --- a/src/com/owncloud/android/ui/preview/PreviewImageFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewImageFragment.java @@ -286,7 +286,7 @@ public class PreviewImageFragment extends FileFragment { public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_share_file: { - mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile()); + mContainerActivity.getFileOperationsHelper().shareFileWithLinkOLD(getFile()); return true; } case R.id.action_share_with_users: { diff --git a/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java b/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java index 8f813b86c9..ae045f19e3 100644 --- a/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java @@ -350,7 +350,7 @@ public class PreviewMediaFragment extends FileFragment implements switch (item.getItemId()) { case R.id.action_share_file: { stopPreview(false); - mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile()); + mContainerActivity.getFileOperationsHelper().shareFileWithLinkOLD(getFile()); return true; } case R.id.action_share_with_users: { diff --git a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java index a07d3dff6a..8038f331be 100644 --- a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java @@ -299,7 +299,7 @@ public class PreviewTextFragment extends FileFragment { public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_share_file: { - mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile()); + mContainerActivity.getFileOperationsHelper().shareFileWithLinkOLD(getFile()); return true; } case R.id.action_share_with_users: {