1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.files
;
20 import org
.apache
.http
.protocol
.HTTP
;
22 import android
.accounts
.AccountManager
;
23 import android
.content
.Intent
;
24 import android
.net
.Uri
;
25 import android
.support
.v4
.app
.DialogFragment
;
26 import android
.webkit
.MimeTypeMap
;
27 import android
.widget
.Toast
;
29 import com
.owncloud
.android
.R
;
30 import com
.owncloud
.android
.datamodel
.OCFile
;
32 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.Constants
;
33 import com
.owncloud
.android
.lib
.common
.network
.WebdavUtils
;
34 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
35 import com
.owncloud
.android
.services
.OperationsService
;
36 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
37 import com
.owncloud
.android
.ui
.dialog
.ShareLinkToDialog
;
38 import com
.owncloud
.android
.utils
.Log_OC
;
43 * @author David A. Velasco
45 public class FileOperationsHelper
{
47 private static final String TAG
= FileOperationsHelper
.class.getName();
49 private static final String FTAG_CHOOSER_DIALOG
= "CHOOSER_DIALOG";
51 protected FileActivity mFileActivity
= null
;
53 /// Identifier of operation in progress which result shouldn't be lost
54 private long mWaitingForOpId
= Long
.MAX_VALUE
;
56 public FileOperationsHelper(FileActivity fileActivity
) {
57 mFileActivity
= fileActivity
;
61 public void openFile(OCFile file
) {
63 String storagePath
= file
.getStoragePath();
64 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
66 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
67 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), file
.getMimetype());
68 intentForSavedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
70 Intent intentForGuessedMimeType
= null
;
71 if (storagePath
.lastIndexOf('.') >= 0) {
72 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
73 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
74 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
75 intentForGuessedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), guessedMimeType
);
76 intentForGuessedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
80 Intent chooserIntent
= null
;
81 if (intentForGuessedMimeType
!= null
) {
82 chooserIntent
= Intent
.createChooser(intentForGuessedMimeType
, mFileActivity
.getString(R
.string
.actionbar_open_with
));
84 chooserIntent
= Intent
.createChooser(intentForSavedMimeType
, mFileActivity
.getString(R
.string
.actionbar_open_with
));
87 mFileActivity
.startActivity(chooserIntent
);
90 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
95 public void shareFileWithLink(OCFile file
) {
97 if (isSharedSupported()) {
99 String link
= "https://fake.url";
100 Intent intent
= createShareWithLinkIntent(link
);
101 String
[] packagesToExclude
= new String
[] { mFileActivity
.getPackageName() };
102 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(intent
, packagesToExclude
, file
);
103 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
106 Log_OC
.wtf(TAG
, "Trying to share a NULL OCFile");
111 Toast t
= Toast
.makeText(mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
117 public void shareFileWithLinkToApp(OCFile file
, Intent sendIntent
) {
120 mFileActivity
.showLoadingDialog();
122 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
123 service
.setAction(OperationsService
.ACTION_CREATE_SHARE
);
124 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
125 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
126 service
.putExtra(OperationsService
.EXTRA_SEND_INTENT
, sendIntent
);
127 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().newOperation(service
);
130 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
135 private Intent
createShareWithLinkIntent(String link
) {
136 Intent intentToShareLink
= new Intent(Intent
.ACTION_SEND
);
137 intentToShareLink
.putExtra(Intent
.EXTRA_TEXT
, link
);
138 intentToShareLink
.setType(HTTP
.PLAIN_TEXT_TYPE
);
139 return intentToShareLink
;
144 * @return 'True' if the server supports the Share API
146 public boolean isSharedSupported() {
147 if (mFileActivity
.getAccount() != null
) {
148 AccountManager accountManager
= AccountManager
.get(mFileActivity
);
150 String version
= accountManager
.getUserData(mFileActivity
.getAccount(), Constants
.KEY_OC_VERSION
);
151 return (new OwnCloudVersion(version
)).isSharedSupported();
157 public void unshareFileWithLink(OCFile file
) {
159 if (isSharedSupported()) {
161 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
162 service
.setAction(OperationsService
.ACTION_UNSHARE
);
163 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
164 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
165 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().newOperation(service
);
167 mFileActivity
.showLoadingDialog();
171 Toast t
= Toast
.makeText(mFileActivity
, mFileActivity
.getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
177 public void sendDownloadedFile(OCFile file
) {
179 Intent sendIntent
= new Intent(android
.content
.Intent
.ACTION_SEND
);
181 sendIntent
.setType(file
.getMimetype());
182 sendIntent
.putExtra(Intent
.EXTRA_STREAM
, Uri
.parse("file://" + file
.getStoragePath()));
183 sendIntent
.putExtra(Intent
.ACTION_SEND
, true
); // Send Action
185 // Show dialog, without the own app
186 String
[] packagesToExclude
= new String
[] { mFileActivity
.getPackageName() };
187 DialogFragment chooserDialog
= ShareLinkToDialog
.newInstance(sendIntent
, packagesToExclude
, file
);
188 chooserDialog
.show(mFileActivity
.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG
);
191 Log_OC
.wtf(TAG
, "Trying to send a NULL OCFile");
196 public void syncFile(OCFile file
) {
198 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
199 service
.setAction(OperationsService
.ACTION_SYNC_FILE
);
200 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
201 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
202 service
.putExtra(OperationsService
.EXTRA_SYNC_FILE_CONTENTS
, true
);
203 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().newOperation(service
);
205 mFileActivity
.showLoadingDialog();
209 public void renameFile(OCFile file
, String newFilename
) {
211 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
212 service
.setAction(OperationsService
.ACTION_RENAME
);
213 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
214 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
215 service
.putExtra(OperationsService
.EXTRA_NEWNAME
, newFilename
);
216 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().newOperation(service
);
218 mFileActivity
.showLoadingDialog();
222 public void removeFile(OCFile file
, boolean onlyLocalCopy
) {
224 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
225 service
.setAction(OperationsService
.ACTION_REMOVE
);
226 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
227 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, file
.getRemotePath());
228 service
.putExtra(OperationsService
.EXTRA_REMOVE_ONLY_LOCAL
, onlyLocalCopy
);
229 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().newOperation(service
);
231 mFileActivity
.showLoadingDialog();
235 public void createFolder(String remotePath
, boolean createFullPath
) {
237 Intent service
= new Intent(mFileActivity
, OperationsService
.class);
238 service
.setAction(OperationsService
.ACTION_CREATE_FOLDER
);
239 service
.putExtra(OperationsService
.EXTRA_ACCOUNT
, mFileActivity
.getAccount());
240 service
.putExtra(OperationsService
.EXTRA_REMOTE_PATH
, remotePath
);
241 service
.putExtra(OperationsService
.EXTRA_CREATE_FULL_PATH
, createFullPath
);
242 mWaitingForOpId
= mFileActivity
.getOperationsServiceBinder().newOperation(service
);
244 mFileActivity
.showLoadingDialog();
248 public long getOpIdWaitingFor() {
249 return mWaitingForOpId
;
253 public void setOpIdWaitingFor(long waitingForOpId
) {
254 mWaitingForOpId
= waitingForOpId
;