FileOperationsHelper.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.common.accounts.AccountUtils.Constants;
  28. import com.owncloud.android.lib.common.network.WebdavUtils;
  29. import com.owncloud.android.lib.resources.status.OwnCloudVersion;
  30. import com.owncloud.android.services.OperationsService;
  31. import com.owncloud.android.ui.activity.FileActivity;
  32. import com.owncloud.android.ui.dialog.ShareLinkToDialog;
  33. import com.owncloud.android.utils.Log_OC;
  34. /**
  35. *
  36. * @author masensio
  37. * @author David A. Velasco
  38. */
  39. public class FileOperationsHelper {
  40. private static final String TAG = FileOperationsHelper.class.getName();
  41. private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
  42. public void openFile(OCFile file, FileActivity callerActivity) {
  43. if (file != null) {
  44. String storagePath = file.getStoragePath();
  45. String encodedStoragePath = WebdavUtils.encodePath(storagePath);
  46. Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
  47. intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
  48. intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  49. Intent intentForGuessedMimeType = null;
  50. if (storagePath.lastIndexOf('.') >= 0) {
  51. String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
  52. if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
  53. intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
  54. intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
  55. intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  56. }
  57. }
  58. Intent chooserIntent = null;
  59. if (intentForGuessedMimeType != null) {
  60. chooserIntent = Intent.createChooser(intentForGuessedMimeType, callerActivity.getString(R.string.actionbar_open_with));
  61. } else {
  62. chooserIntent = Intent.createChooser(intentForSavedMimeType, callerActivity.getString(R.string.actionbar_open_with));
  63. }
  64. callerActivity.startActivity(chooserIntent);
  65. } else {
  66. Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
  67. }
  68. }
  69. public void shareFileWithLink(OCFile file, FileActivity callerActivity) {
  70. if (isSharedSupported(callerActivity)) {
  71. if (file != null) {
  72. String link = "https://fake.url";
  73. Intent intent = createShareWithLinkIntent(link);
  74. String[] packagesToExclude = new String[] { callerActivity.getPackageName() };
  75. DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intent, packagesToExclude, file);
  76. chooserDialog.show(callerActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
  77. } else {
  78. Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
  79. }
  80. } else {
  81. // Show a Message
  82. Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
  83. t.show();
  84. }
  85. }
  86. public void shareFileWithLinkToApp(OCFile file, Intent sendIntent, FileActivity callerActivity) {
  87. if (file != null) {
  88. callerActivity.showLoadingDialog();
  89. Intent service = new Intent(callerActivity, OperationsService.class);
  90. service.setAction(OperationsService.ACTION_CREATE_SHARE);
  91. service.putExtra(OperationsService.EXTRA_ACCOUNT, callerActivity.getAccount());
  92. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  93. service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
  94. callerActivity.startService(service);
  95. } else {
  96. Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
  97. }
  98. }
  99. private Intent createShareWithLinkIntent(String link) {
  100. Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
  101. intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
  102. intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
  103. return intentToShareLink;
  104. }
  105. /**
  106. * @return 'True' if the server supports the Share API
  107. */
  108. public boolean isSharedSupported(FileActivity callerActivity) {
  109. if (callerActivity.getAccount() != null) {
  110. AccountManager accountManager = AccountManager.get(callerActivity);
  111. String version = accountManager.getUserData(callerActivity.getAccount(), Constants.KEY_OC_VERSION);
  112. String versionString = accountManager.getUserData(callerActivity.getAccount(), Constants.KEY_OC_VERSION_STRING);
  113. return (new OwnCloudVersion(version, versionString)).isSharedSupported();
  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. Intent service = new Intent(callerActivity, OperationsService.class);
  122. service.setAction(OperationsService.ACTION_UNSHARE);
  123. service.putExtra(OperationsService.EXTRA_ACCOUNT, callerActivity.getAccount());
  124. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  125. callerActivity.startService(service);
  126. callerActivity.showLoadingDialog();
  127. } else {
  128. // Show a Message
  129. Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
  130. t.show();
  131. }
  132. }
  133. public void sendDownloadedFile(OCFile file, FileActivity callerActivity) {
  134. if (file != null) {
  135. Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
  136. // set MimeType
  137. sendIntent.setType(file.getMimetype());
  138. sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getStoragePath()));
  139. sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
  140. // Show dialog, without the own app
  141. String[] packagesToExclude = new String[] { callerActivity.getPackageName() };
  142. DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude, file);
  143. chooserDialog.show(callerActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
  144. } else {
  145. Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
  146. }
  147. }
  148. }