FileOperationsHelper.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.support.v4.app.DialogFragment;
  23. import android.webkit.MimeTypeMap;
  24. import android.widget.Toast;
  25. import com.owncloud.android.R;
  26. import com.owncloud.android.datamodel.OCFile;
  27. import com.owncloud.android.lib.accounts.OwnCloudAccount;
  28. import com.owncloud.android.lib.network.webdav.WebdavUtils;
  29. import com.owncloud.android.services.OperationsService;
  30. import com.owncloud.android.ui.activity.FileActivity;
  31. import com.owncloud.android.ui.dialog.ActivityChooserDialog;
  32. import com.owncloud.android.utils.Log_OC;
  33. /**
  34. *
  35. * @author masensio
  36. * @author David A. Velasco
  37. */
  38. public class FileOperationsHelper {
  39. private static final String TAG = FileOperationsHelper.class.getName();
  40. private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
  41. public void openFile(OCFile file, FileActivity callerActivity) {
  42. if (file != null) {
  43. String storagePath = file.getStoragePath();
  44. String encodedStoragePath = WebdavUtils.encodePath(storagePath);
  45. Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
  46. intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
  47. intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  48. Intent intentForGuessedMimeType = null;
  49. if (storagePath.lastIndexOf('.') >= 0) {
  50. String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
  51. if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
  52. intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
  53. intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
  54. intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  55. }
  56. }
  57. Intent chooserIntent = null;
  58. if (intentForGuessedMimeType != null) {
  59. chooserIntent = Intent.createChooser(intentForGuessedMimeType, callerActivity.getString(R.string.actionbar_open_with));
  60. } else {
  61. chooserIntent = Intent.createChooser(intentForSavedMimeType, callerActivity.getString(R.string.actionbar_open_with));
  62. }
  63. callerActivity.startActivity(chooserIntent);
  64. } else {
  65. Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
  66. }
  67. }
  68. public void shareFileWithLink(OCFile file, FileActivity callerActivity) {
  69. if (isSharedSupported(callerActivity)) {
  70. if (file != null) {
  71. String link = "https://fake.url";
  72. Intent intent = createShareWithLinkIntent(link);
  73. String[] packagesToExclude = new String[] { callerActivity.getPackageName() };
  74. DialogFragment chooserDialog = ActivityChooserDialog.newInstance(intent, packagesToExclude, file);
  75. chooserDialog.show(callerActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
  76. } else {
  77. Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
  78. }
  79. } else {
  80. // Show a Message
  81. Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
  82. t.show();
  83. }
  84. }
  85. public void shareFileWithLinkToApp(OCFile file, Intent sendIntent, FileActivity callerActivity) {
  86. if (file != null) {
  87. callerActivity.showLoadingDialog();
  88. Intent service = new Intent(callerActivity, OperationsService.class);
  89. service.setAction(OperationsService.ACTION_CREATE_SHARE);
  90. service.putExtra(OperationsService.EXTRA_ACCOUNT, callerActivity.getAccount());
  91. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  92. service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
  93. callerActivity.startService(service);
  94. } else {
  95. Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
  96. }
  97. }
  98. private Intent createShareWithLinkIntent(String link) {
  99. Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
  100. intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
  101. intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
  102. return intentToShareLink;
  103. }
  104. /**
  105. * @return 'True' if the server supports the Share API
  106. */
  107. public boolean isSharedSupported(FileActivity callerActivity) {
  108. if (callerActivity.getAccount() != null) {
  109. AccountManager accountManager = AccountManager.get(callerActivity);
  110. return Boolean.parseBoolean(accountManager.getUserData(callerActivity.getAccount(), OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API));
  111. }
  112. return false;
  113. }
  114. public void unshareFileWithLink(OCFile file, FileActivity callerActivity) {
  115. if (isSharedSupported(callerActivity)) {
  116. // Unshare the file
  117. Intent service = new Intent(callerActivity, OperationsService.class);
  118. service.setAction(OperationsService.ACTION_UNSHARE);
  119. service.putExtra(OperationsService.EXTRA_ACCOUNT, callerActivity.getAccount());
  120. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  121. callerActivity.startService(service);
  122. callerActivity.showLoadingDialog();
  123. } else {
  124. // Show a Message
  125. Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
  126. t.show();
  127. }
  128. }
  129. }