Fixed. App crash when setting a path without slash
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / MoveActivity.java
index 89de5e9..2d6824c 100644 (file)
@@ -1,3 +1,20 @@
+/* ownCloud Android client application
+ *   Copyright (C) 2012-2014 ownCloud Inc.
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License version 2,
+ *   as published by the Free Software Foundation.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
 package com.owncloud.android.ui.activity;
 
 import java.io.IOException;
@@ -10,9 +27,11 @@ import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.res.Resources.NotFoundException;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentTransaction;
+import android.support.v4.widget.SwipeRefreshLayout;
 import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
@@ -34,16 +53,23 @@ import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundExce
 import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
+import com.owncloud.android.operations.CreateFolderOperation;
 import com.owncloud.android.operations.SynchronizeFolderOperation;
 import com.owncloud.android.syncadapter.FileSyncAdapter;
 import com.owncloud.android.ui.dialog.CreateFolderDialogFragment;
 import com.owncloud.android.ui.fragment.FileFragment;
-import com.owncloud.android.ui.fragment.MoveFileListFragment;
+import com.owncloud.android.ui.fragment.OCFileListFragment;
 import com.owncloud.android.utils.DisplayUtils;
-import com.owncloud.android.utils.Log_OC;
+import com.owncloud.android.utils.ErrorMessageAdapter;
+import com.owncloud.android.lib.common.utils.Log_OC;
 
 public class MoveActivity extends HookActivity implements FileFragment.ContainerActivity, 
-    OnClickListener{
+    OnClickListener, SwipeRefreshLayout.OnRefreshListener {
+
+    public static final String EXTRA_CURRENT_FOLDER = UploadFilesActivity.class.getCanonicalName() + ".EXTRA_CURRENT_FOLDER";
+    public static final String EXTRA_TARGET_FILE = UploadFilesActivity.class.getCanonicalName() + "EXTRA_TARGET_FILE";
+
+    public static final int RESULT_OK_AND_MOVE = 1;
     
     private SyncBroadcastReceiver mSyncBroadcastReceiver;
 
@@ -116,10 +142,10 @@ public class MoveActivity extends HookActivity implements FileFragment.Container
             }
             
             if (!stateWasRecovered) {
-                MoveFileListFragment listOfFolders = getListOfFilesFragment(); 
+                OCFileListFragment listOfFolders = getListOfFilesFragment(); 
                 listOfFolders.listDirectory(folder);   
                 
-                startSyncFolderOperation(folder);
+                startSyncFolderOperation(folder, false);
             }
             
             updateNavigationElementsInActionBar();
@@ -127,7 +153,11 @@ public class MoveActivity extends HookActivity implements FileFragment.Container
     }
 
     private void createFragments() {
-        MoveFileListFragment listOfFiles = new MoveFileListFragment();
+        OCFileListFragment listOfFiles = new OCFileListFragment();
+        Bundle args = new Bundle();
+        args.putBoolean(OCFileListFragment.ARG_JUST_FOLDERS, true);
+        args.putBoolean(OCFileListFragment.ARG_ALLOW_CONTEXTUAL_ACTIONS, false);
+        listOfFiles.setArguments(args);
         FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
         transaction.add(R.id.fragment_container, listOfFiles, TAG_LIST_OF_FOLDERS);
         transaction.commit();
@@ -138,23 +168,23 @@ public class MoveActivity extends HookActivity implements FileFragment.Container
      * loading or folder is empty
      */
     private void setBackgroundText() {
-        MoveFileListFragment MoveFileListFragment = getListOfFilesFragment();
-        if (MoveFileListFragment != null) {
+        OCFileListFragment listFragment = getListOfFilesFragment();
+        if (listFragment != null) {
             int message = R.string.file_list_loading;
             if (!mSyncInProgress) {
                 // In case folder list is empty
                 message = R.string.file_list_empty_moving;
             }
-            MoveFileListFragment.setMessageForEmptyList(getString(message));
+            listFragment.setMessageForEmptyList(getString(message));
         } else {
-            Log.e(TAG, "MoveFileListFragment is null");
+            Log.e(TAG, "OCFileListFragment is null");
         }
     }
 
-    private MoveFileListFragment getListOfFilesFragment() {
+    private OCFileListFragment getListOfFilesFragment() {
         Fragment listOfFiles = getSupportFragmentManager().findFragmentByTag(MoveActivity.TAG_LIST_OF_FOLDERS);
         if (listOfFiles != null) {
-            return (MoveFileListFragment)listOfFiles;
+            return (OCFileListFragment)listOfFiles;
         }
         Log_OC.wtf(TAG, "Access to unexisting list of files fragment!!");
         return null;
@@ -171,12 +201,12 @@ public class MoveActivity extends HookActivity implements FileFragment.Container
         setFile(directory);
         updateNavigationElementsInActionBar();
         // Sync Folder
-        startSyncFolderOperation(directory);
+        startSyncFolderOperation(directory, false);
         
     }
 
     
-    public void startSyncFolderOperation(OCFile folder) {
+    public void startSyncFolderOperation(OCFile folder, boolean ignoreETag) {
         long currentSyncTime = System.currentTimeMillis(); 
         
         mSyncInProgress = true;
@@ -186,6 +216,7 @@ public class MoveActivity extends HookActivity implements FileFragment.Container
                                                                         currentSyncTime, 
                                                                         false,
                                                                         getFileOperationsHelper().isSharedSupported(),
+                                                                        ignoreETag,
                                                                         getStorageManager(), 
                                                                         getAccount(), 
                                                                         getApplicationContext()
@@ -280,26 +311,26 @@ public class MoveActivity extends HookActivity implements FileFragment.Container
     }
     
     protected void refreshListOfFilesFragment() {
-        MoveFileListFragment fileListFragment = getListOfFilesFragment();
+        OCFileListFragment fileListFragment = getListOfFilesFragment();
         if (fileListFragment != null) { 
             fileListFragment.listDirectory();
         }
     }
 
     public void browseToRoot() {
-        MoveFileListFragment listOfFiles = getListOfFilesFragment(); 
+        OCFileListFragment listOfFiles = getListOfFilesFragment(); 
         if (listOfFiles != null) {  // should never be null, indeed
             OCFile root = getStorageManager().getFileByPath(OCFile.ROOT_PATH);
             listOfFiles.listDirectory(root);
             setFile(listOfFiles.getCurrentFile());
             updateNavigationElementsInActionBar();
-            startSyncFolderOperation(root);
+            startSyncFolderOperation(root, false);
         }
     }
 
     @Override
     public void onBackPressed() {
-        MoveFileListFragment listOfFiles = getListOfFilesFragment();
+        OCFileListFragment listOfFiles = getListOfFilesFragment();
         if (listOfFiles != null) {  // should never be null, indeed
             int levelsUp = listOfFiles.onBrowseUp();
             if (levelsUp == 0) {
@@ -339,16 +370,59 @@ public class MoveActivity extends HookActivity implements FileFragment.Container
         if (v == mCancelBtn) {
             finish();
         } else if (v == mChooseBtn) {
-            // TODO request to move, OR save selected folder as a result and let request for caller
-            Toast.makeText( MoveActivity.this, 
-                            "TODO: MOVE IMPLEMENTATION", 
-                            Toast.LENGTH_LONG)
-                .show();
+            Intent i = getIntent();
+            OCFile targetFile = (OCFile) i.getParcelableExtra(MoveActivity.EXTRA_TARGET_FILE);
+
+            Intent data = new Intent();
+            data.putExtra(EXTRA_CURRENT_FOLDER, getCurrentFolder());
+            data.putExtra(EXTRA_TARGET_FILE, targetFile);
+            setResult(RESULT_OK_AND_MOVE, data);
             finish();
         }
     }
     
     
+    @Override
+    public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
+        super.onRemoteOperationFinish(operation, result);
+        
+        if (operation instanceof CreateFolderOperation) {
+            onCreateFolderOperationFinish((CreateFolderOperation)operation, result);
+            
+        }
+    }
+    
+    
+    /**
+     * Updates the view associated to the activity after the finish of an operation trying 
+     * to create a new folder.
+     * 
+     * @param operation     Creation operation performed.
+     * @param result        Result of the creation.
+     */
+    private void onCreateFolderOperationFinish(
+            CreateFolderOperation operation, RemoteOperationResult result
+            ) {
+        
+        if (result.isSuccess()) {
+            dismissLoadingDialog();
+            refreshListOfFilesFragment();
+        } else {
+            dismissLoadingDialog();
+            try {
+                Toast msg = Toast.makeText(MoveActivity.this, 
+                        ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()), 
+                        Toast.LENGTH_LONG); 
+                msg.show();
+
+            } catch (NotFoundException e) {
+                Log_OC.e(TAG, "Error while trying to show fail message " , e);
+            }
+        }
+    }
+    
+    
+    
     private class SyncBroadcastReceiver extends BroadcastReceiver {
 
         /**
@@ -376,7 +450,7 @@ public class MoveActivity extends HookActivity implements FileFragment.Container
                         if (currentDir == null) {
                             // current folder was removed from the server 
                             Toast.makeText( MoveActivity.this, 
-                                            String.format(getString(R.string.sync_current_folder_was_removed), "PLACEHOLDER"), 
+                                            String.format(getString(R.string.sync_current_folder_was_removed), getCurrentFolder().getFileName()), 
                                             Toast.LENGTH_LONG)
                                 .show();
                             browseToRoot();
@@ -388,7 +462,7 @@ public class MoveActivity extends HookActivity implements FileFragment.Container
                             }
 
                             if (synchFolderRemotePath != null && currentDir.getRemotePath().equals(synchFolderRemotePath)) {
-                                MoveFileListFragment fileListFragment = getListOfFilesFragment();
+                                OCFileListFragment fileListFragment = getListOfFilesFragment();
                                 if (fileListFragment != null) {
                                     fileListFragment.listDirectory(currentDir);
                                 }
@@ -481,4 +555,15 @@ public class MoveActivity extends HookActivity implements FileFragment.Container
     }
 
 
+    @Override
+    public void onRefresh() {
+        OCFileListFragment listOfFiles = getListOfFilesFragment();
+        if (listOfFiles != null) {
+            OCFile folder = listOfFiles.getCurrentFile();
+            if (folder != null) {
+                startSyncFolderOperation(folder, true);
+            }
+        }
+    }
+
 }