OC-3121: (fix bug) An empty message is sent, when renaming a file in the web of a...
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / FileOperationsHelper.java
index ab8395c..e413cde 100644 (file)
@@ -17,6 +17,8 @@
 
 package com.owncloud.android.files;
 
 
 package com.owncloud.android.files;
 
+import java.io.File;
+import java.io.IOException;
 import org.apache.http.protocol.HTTP;
 
 import android.accounts.AccountManager;
 import org.apache.http.protocol.HTTP;
 
 import android.accounts.AccountManager;
@@ -28,12 +30,14 @@ import android.widget.Toast;
 
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
 
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.lib.accounts.OwnCloudAccount;
-import com.owncloud.android.lib.network.webdav.WebdavUtils;
-import com.owncloud.android.lib.operations.common.ShareType;
-import com.owncloud.android.operations.CreateShareOperation;
+
+import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
+import com.owncloud.android.lib.common.network.WebdavUtils;
+import com.owncloud.android.lib.resources.status.OwnCloudVersion;
+import com.owncloud.android.services.OperationsService;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.activity.FileActivity;
-import com.owncloud.android.ui.dialog.ActivityChooserDialog;
+import com.owncloud.android.ui.dialog.ShareLinkToDialog;
+import com.owncloud.android.utils.FileStorageUtils;
 import com.owncloud.android.utils.Log_OC;
 
 /**
 import com.owncloud.android.utils.Log_OC;
 
 /**
@@ -45,7 +49,9 @@ public class FileOperationsHelper {
 
     private static final String TAG = FileOperationsHelper.class.getName();
     
 
     private static final String TAG = FileOperationsHelper.class.getName();
     
-    private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG"; 
+    private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
+    
+    public final static int REQUEST_CODE_FILE_OPEN_HELPER = 100;
 
     
     public void openFile(OCFile file, FileActivity callerActivity) {
 
     
     public void openFile(OCFile file, FileActivity callerActivity) {
@@ -89,7 +95,7 @@ public class FileOperationsHelper {
                 String link = "https://fake.url";
                 Intent intent = createShareWithLinkIntent(link);
                 String[] packagesToExclude = new String[] { callerActivity.getPackageName() };
                 String link = "https://fake.url";
                 Intent intent = createShareWithLinkIntent(link);
                 String[] packagesToExclude = new String[] { callerActivity.getPackageName() };
-                DialogFragment chooserDialog = ActivityChooserDialog.newInstance(intent, packagesToExclude, file);
+                DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intent, packagesToExclude, file);
                 chooserDialog.show(callerActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
                 
             } else {
                 chooserDialog.show(callerActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
                 
             } else {
@@ -108,12 +114,13 @@ public class FileOperationsHelper {
         
         if (file != null) {
             callerActivity.showLoadingDialog();
         
         if (file != null) {
             callerActivity.showLoadingDialog();
-            CreateShareOperation createShare = new CreateShareOperation(file.getRemotePath(), ShareType.PUBLIC_LINK, "", false, "", 1, sendIntent);
-            createShare.execute(callerActivity.getStorageManager(), 
-                                callerActivity, 
-                                callerActivity.getRemoteOperationListener(), 
-                                callerActivity.getHandler(), 
-                                callerActivity);
+            
+            Intent service = new Intent(callerActivity, OperationsService.class);
+            service.setAction(OperationsService.ACTION_CREATE_SHARE);
+            service.putExtra(OperationsService.EXTRA_ACCOUNT, callerActivity.getAccount());
+            service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
+            service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
+            callerActivity.startService(service);
             
         } else {
             Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
             
         } else {
             Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
@@ -135,9 +142,88 @@ public class FileOperationsHelper {
     public boolean isSharedSupported(FileActivity callerActivity) {
         if (callerActivity.getAccount() != null) {
             AccountManager accountManager = AccountManager.get(callerActivity);
     public boolean isSharedSupported(FileActivity callerActivity) {
         if (callerActivity.getAccount() != null) {
             AccountManager accountManager = AccountManager.get(callerActivity);
-            return Boolean.parseBoolean(accountManager.getUserData(callerActivity.getAccount(), OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API));
+
+            String version = accountManager.getUserData(callerActivity.getAccount(), Constants.KEY_OC_VERSION);
+            String versionString = accountManager.getUserData(callerActivity.getAccount(), Constants.KEY_OC_VERSION_STRING);
+            return (new OwnCloudVersion(version, versionString)).isSharedSupported();
+            //return Boolean.parseBoolean(accountManager.getUserData(callerActivity.getAccount(), OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API));
         }
         return false;
     }
         }
         return false;
     }
+    
+    
+    public void unshareFileWithLink(OCFile file, FileActivity callerActivity) {
+        
+        if (isSharedSupported(callerActivity)) {
+            // Unshare the file
+            Intent service = new Intent(callerActivity, OperationsService.class);
+            service.setAction(OperationsService.ACTION_UNSHARE);
+            service.putExtra(OperationsService.EXTRA_ACCOUNT, callerActivity.getAccount());
+            service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
+            callerActivity.startService(service);
+            
+            callerActivity.showLoadingDialog();
+            
+        } else {
+            // Show a Message
+            Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
+            t.show();
+            
+        }
+    }
+    
+    public void sendDownloadedFile(OCFile file, FileActivity callerActivity) {
+        if (file != null) {
+            Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
+            // set MimeType
+            sendIntent.setType(file.getMimetype());
+            sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getStoragePath()));
+            sendIntent.putExtra(Intent.ACTION_SEND, true);      // Send Action
+            
+            // Show dialog, without the own app
+            String[] packagesToExclude = new String[] { callerActivity.getPackageName() };
+            DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude, file);
+            chooserDialog.show(callerActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
+
+        } else {
+            Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
+        }
+    }
+    
+    public void sendFileToApp(Intent sendIntent, FileActivity callerActivity) {
+        Uri filePath = sendIntent.getParcelableExtra(Intent.EXTRA_STREAM);
+        File file = new File(filePath.getPath());
+        Log_OC.d(TAG,  "FILE " + filePath.getPath());
+        if (file.exists()) {
+            File folder = new File(FileStorageUtils.getTemporalPath(callerActivity.getAccount().name) + "/send");
+            boolean success = true;
+            if (!folder.exists()) {
+                success = folder.mkdir();
+            }
+            if (success) {
+                File tmpFile = new File(folder.getAbsolutePath()+ "/" + file.getName());
+                try {
+                    tmpFile.createNewFile();
+                    FileStorageUtils.copyFile(file, tmpFile);
+                } catch (IOException e) {
+                    Log_OC.e(TAG,  "An error occurred while it was trying to copy in a temporal folder " + e.getMessage());
+                }
+                // Update Uri
+                Uri uri = Uri.fromFile(tmpFile);
+                sendIntent.removeExtra(Intent.EXTRA_STREAM);
+                sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
+                callerActivity.startActivityForResult(sendIntent, REQUEST_CODE_FILE_OPEN_HELPER);
+            }
+        } else {
+            // Show a Message
+            Toast t = Toast.makeText(callerActivity, callerActivity.getString(R.string.send_file_missing_file), Toast.LENGTH_LONG);
+            t.show();
+            Log_OC.d(TAG,  "Missing file");
+        }
 
 
+    }
+    
+    
+    
+    
 }
 }