FileOperationsHelper.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2014 ownCloud Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package com.owncloud.android.files;
  18. import org.apache.http.protocol.HTTP;
  19. import android.accounts.AccountManager;
  20. import android.content.Intent;
  21. import android.net.Uri;
  22. import android.sax.StartElementListener;
  23. import android.support.v4.app.DialogFragment;
  24. import android.webkit.MimeTypeMap;
  25. import android.widget.Toast;
  26. import com.owncloud.android.R;
  27. import com.owncloud.android.datamodel.OCFile;
  28. import com.owncloud.android.lib.accounts.OwnCloudAccount;
  29. import com.owncloud.android.lib.network.webdav.WebdavUtils;
  30. import com.owncloud.android.lib.operations.common.ShareType;
  31. import com.owncloud.android.operations.CreateShareOperation;
  32. import com.owncloud.android.operations.UnshareLinkOperation;
  33. import com.owncloud.android.services.OperationsService;
  34. import com.owncloud.android.ui.activity.FileActivity;
  35. import com.owncloud.android.ui.dialog.ActivityChooserDialog;
  36. import com.owncloud.android.utils.Log_OC;
  37. /**
  38. *
  39. * @author masensio
  40. * @author David A. Velasco
  41. */
  42. public class FileOperationsHelper {
  43. private static final String TAG = FileOperationsHelper.class.getName();
  44. private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
  45. public void openFile(OCFile file, FileActivity callerActivity) {
  46. if (file != null) {
  47. String storagePath = file.getStoragePath();
  48. String encodedStoragePath = WebdavUtils.encodePath(storagePath);
  49. Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
  50. intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
  51. intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  52. Intent intentForGuessedMimeType = null;
  53. if (storagePath.lastIndexOf('.') >= 0) {
  54. String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
  55. if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
  56. intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
  57. intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
  58. intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  59. }
  60. }
  61. Intent chooserIntent = null;
  62. if (intentForGuessedMimeType != null) {
  63. chooserIntent = Intent.createChooser(intentForGuessedMimeType, callerActivity.getString(R.string.actionbar_open_with));
  64. } else {
  65. chooserIntent = Intent.createChooser(intentForSavedMimeType, callerActivity.getString(R.string.actionbar_open_with));
  66. }
  67. callerActivity.startActivity(chooserIntent);
  68. } else {
  69. Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
  70. }
  71. }
  72. public void shareFileWithLink(OCFile file, FileActivity callerActivity) {
  73. if (isSharedSupported(callerActivity)) {
  74. if (file != null) {
  75. String link = "https://fake.url";
  76. Intent intent = createShareWithLinkIntent(link);
  77. String[] packagesToExclude = new String[] { callerActivity.getPackageName() };
  78. DialogFragment chooserDialog = ActivityChooserDialog.newInstance(intent, packagesToExclude, file);
  79. chooserDialog.show(callerActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
  80. } else {
  81. Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
  82. }
  83. } else {
  84. // Show a Message
  85. Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
  86. t.show();
  87. }
  88. }
  89. public void shareFileWithLinkToApp(OCFile file, Intent sendIntent, FileActivity callerActivity) {
  90. if (file != null) {
  91. callerActivity.showLoadingDialog();
  92. Intent service = new Intent(callerActivity, OperationsService.class);
  93. service.setAction(OperationsService.ACTION_CREATE_SHARE);
  94. service.putExtra(OperationsService.EXTRA_ACCOUNT, callerActivity.getAccount());
  95. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  96. service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
  97. callerActivity.startService(service);
  98. } else {
  99. Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
  100. }
  101. }
  102. private Intent createShareWithLinkIntent(String link) {
  103. Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
  104. intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
  105. intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
  106. return intentToShareLink;
  107. }
  108. /**
  109. * @return 'True' if the server supports the Share API
  110. */
  111. public boolean isSharedSupported(FileActivity callerActivity) {
  112. if (callerActivity.getAccount() != null) {
  113. AccountManager accountManager = AccountManager.get(callerActivity);
  114. return Boolean.parseBoolean(accountManager.getUserData(callerActivity.getAccount(), OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API));
  115. }
  116. return false;
  117. }
  118. public void unshareFileWithLink(OCFile file, FileActivity callerActivity) {
  119. if (isSharedSupported(callerActivity)) {
  120. // Unshare the file
  121. UnshareLinkOperation unshare = new UnshareLinkOperation(file, callerActivity);
  122. unshare.execute(callerActivity.getStorageManager(),
  123. callerActivity,
  124. callerActivity.getRemoteOperationListener(),
  125. callerActivity.getHandler(),
  126. callerActivity);
  127. callerActivity.showLoadingDialog();
  128. } else {
  129. // Show a Message
  130. Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
  131. t.show();
  132. }
  133. }
  134. }