Merge branch 'develop' into create_folder_during_upload_pr_701_with_develop
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / Uploader.java
index f6ff598..8a01972 100644 (file)
@@ -1,6 +1,9 @@
-/* ownCloud Android client application
+/**
+ *   ownCloud Android client application
+ *
+ *   @author Bartek Przybylski
  *   Copyright (C) 2012  Bartek Przybylski
- *   Copyright (C) 2012-2013 ownCloud Inc.
+ *   Copyright (C) 2015 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,
@@ -26,14 +29,6 @@ import java.util.List;
 import java.util.Stack;
 import java.util.Vector;
 
-import com.owncloud.android.MainApp;
-import com.owncloud.android.R;
-import com.owncloud.android.authentication.AccountAuthenticator;
-import com.owncloud.android.datamodel.FileDataStorageManager;
-import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.files.services.FileUploader;
-import com.owncloud.android.lib.common.utils.Log_OC;
-
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.app.AlertDialog;
@@ -46,6 +41,7 @@ import android.content.DialogInterface.OnCancelListener;
 import android.content.DialogInterface.OnClickListener;
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.content.res.Resources.NotFoundException;
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.Bundle;
@@ -59,36 +55,31 @@ import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemClickListener;
 import android.widget.Button;
 import android.widget.EditText;
+import android.widget.ListView;
 import android.widget.SimpleAdapter;
 import android.widget.Toast;
 
 import com.actionbarsherlock.app.ActionBar;
-import com.actionbarsherlock.app.SherlockListActivity;
+import com.actionbarsherlock.view.MenuItem;
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountAuthenticator;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.services.FileUploader;
-import com.owncloud.android.utils.DisplayUtils;
+import com.owncloud.android.lib.common.operations.RemoteOperation;
+import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.utils.Log_OC;
-
-import java.io.File;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Stack;
-import java.util.Vector;
+import com.owncloud.android.operations.CreateFolderOperation;
+import com.owncloud.android.ui.dialog.CreateFolderDialogFragment;
+import com.owncloud.android.utils.DisplayUtils;
+import com.owncloud.android.utils.ErrorMessageAdapter;
 
 
 /**
  * This can be used to upload things to an ownCloud instance.
- * 
- * @author Bartek Przybylski
- * 
  */
-public class Uploader extends SherlockListActivity implements OnItemClickListener, android.view.View.OnClickListener {
+public class Uploader extends FileActivity implements OnItemClickListener, android.view.View.OnClickListener {
     private static final String TAG = "ownCloudUploader";
 
     private Account mAccount;
@@ -99,8 +90,7 @@ public class Uploader extends SherlockListActivity implements OnItemClickListene
     private String mUploadPath;
     private FileDataStorageManager mStorageManager;
     private OCFile mFile;
-    private boolean mSaveUploadLocation;
-
+    
     private final static int DIALOG_NO_ACCOUNT = 0;
     private final static int DIALOG_WAITING = 1;
     private final static int DIALOG_NO_STREAM = 2;
@@ -113,7 +103,7 @@ public class Uploader extends SherlockListActivity implements OnItemClickListene
         super.onCreate(savedInstanceState);
         mParents = new Stack<String>();
 
-        ActionBar actionBar = getSherlock().getActionBar();
+        ActionBar actionBar = getSupportActionBar();
         actionBar.setIcon(DisplayUtils.getSeasonalIconId());
 
         if (prepareStreamsToUpload()) {
@@ -126,39 +116,13 @@ public class Uploader extends SherlockListActivity implements OnItemClickListene
                 Log_OC.i(TAG, "More then one ownCloud is available");
                 showDialog(DIALOG_MULTIPLE_ACCOUNT);
             } else {
-
                 mAccount = accounts[0];
                 mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
-
-                SharedPreferences appPreferences = PreferenceManager
-                        .getDefaultSharedPreferences(getApplicationContext());
-
-                mSaveUploadLocation = appPreferences.getBoolean("save_last_upload_location", false);
-
-                //If the users has enabled last upload path saving then populate mParents with the previous path
-                if(mSaveUploadLocation)
-                {
-                    String last_path = appPreferences.getString("last_upload_path", "");
-                    // "/" equals root-directory
-                    if(last_path.equals("/")) {
-                        mParents.add("");
-                    }
-                    else{
-                        String[] dir_names = last_path.split("/");
-                        for (String dir : dir_names)
-                            mParents.add(dir);
-                    }
-                    //Make sure that path still exists, if it doesn't pop the stack and try the previous path
-                    while(!mStorageManager.fileExists(generatePath(mParents))){
-                        mParents.pop();
-                    }
-                }
-                else {
-                    mParents.add("");
-                }
-
+                initTargetFolder();
                 populateDirectoryList();
+                
             }
+            
         } else {
             showDialog(DIALOG_NO_STREAM);
         }
@@ -212,7 +176,7 @@ public class Uploader extends SherlockListActivity implements OnItemClickListene
         case DIALOG_MULTIPLE_ACCOUNT:
             CharSequence ac[] = new CharSequence[mAccountManager.getAccountsByType(MainApp.getAccountType()).length];
             for (int i = 0; i < ac.length; ++i) {
-                ac[i] = mAccountManager.getAccountsByType(MainApp.getAccountType())[i].name;
+                ac[i] = DisplayUtils.convertIdn(mAccountManager.getAccountsByType(MainApp.getAccountType())[i].name, false);
             }
             builder.setTitle(R.string.common_choose_account);
             builder.setItems(ac, new OnClickListener() {
@@ -220,6 +184,7 @@ public class Uploader extends SherlockListActivity implements OnItemClickListene
                 public void onClick(DialogInterface dialog, int which) {
                     mAccount = mAccountManager.getAccountsByType(MainApp.getAccountType())[which];
                     mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
+                    initTargetFolder();
                     populateDirectoryList();
                 }
             });
@@ -309,6 +274,13 @@ public class Uploader extends SherlockListActivity implements OnItemClickListene
             uploadFiles();
 
             break;
+            
+        case R.id.uploader_new_folder:
+            CreateFolderDialogFragment dialog = CreateFolderDialogFragment.newInstance(mFile);
+            dialog.show(getSupportFragmentManager(), "createdirdialog");
+            break;
+            
+            
         default:
             throw new IllegalArgumentException("Wrong element clicked");
         }
@@ -338,14 +310,20 @@ public class Uploader extends SherlockListActivity implements OnItemClickListene
 
     private void populateDirectoryList() {
         setContentView(R.layout.uploader_layout);
+        
+        ListView mListView = (ListView) findViewById(android.R.id.list);
 
         String current_dir = mParents.peek();
         if(current_dir.equals("")){
-            getActionBar().setTitle(getString(R.string.default_display_name_for_root_folder));
+            getSupportActionBar().setTitle(getString(R.string.default_display_name_for_root_folder));
         }
         else{
-            getActionBar().setTitle(current_dir);
+            getSupportActionBar().setTitle(current_dir);
         }
+        boolean notRoot = (mParents.size() > 1);
+        ActionBar actionBar = getSupportActionBar();
+        actionBar.setDisplayHomeAsUpEnabled(notRoot);
+        actionBar.setHomeButtonEnabled(notRoot);
 
         String full_path = generatePath(mParents);
         
@@ -367,10 +345,15 @@ public class Uploader extends SherlockListActivity implements OnItemClickListene
                                                 R.layout.uploader_list_item_layout,
                                                 new String[] {"dirname"},
                                                 new int[] {R.id.textView1});
-            setListAdapter(sa);
-            Button btn = (Button) findViewById(R.id.uploader_choose_folder);
-            btn.setOnClickListener(this);
-            getListView().setOnItemClickListener(this);
+            
+            mListView.setAdapter(sa);
+            Button btnChooseFolder = (Button) findViewById(R.id.uploader_choose_folder);
+            btnChooseFolder.setOnClickListener(this);
+            
+            Button btnNewFolder = (Button) findViewById(R.id.uploader_new_folder);
+            btnNewFolder.setOnClickListener(this);
+            
+            mListView.setOnItemClickListener(this);
         }
     }
 
@@ -474,13 +457,11 @@ public class Uploader extends SherlockListActivity implements OnItemClickListene
             intent.putExtra(FileUploader.KEY_ACCOUNT, mAccount);
             startService(intent);
 
-            //If the user has enabled last upload path then save the path to shared preferences
-            if(mSaveUploadLocation){
-                SharedPreferences.Editor appPrefs = PreferenceManager
-                        .getDefaultSharedPreferences(getApplicationContext()).edit();
-                appPrefs.putString("last_upload_path", mUploadPath);
-                appPrefs.apply();
-            }
+            //Save the path to shared preferences
+            SharedPreferences.Editor appPrefs = PreferenceManager
+                    .getDefaultSharedPreferences(getApplicationContext()).edit();
+            appPrefs.putString("last_upload_path", mUploadPath);
+            appPrefs.apply();
 
             finish();
             }
@@ -490,5 +471,88 @@ public class Uploader extends SherlockListActivity implements OnItemClickListene
             Toast.makeText(this, message, Toast.LENGTH_LONG).show();            
         }
     }
+    
+    @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 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();
+            populateDirectoryList();
+        } else {
+            dismissLoadingDialog();
+            try {
+                Toast msg = Toast.makeText(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);
+            }
+        }
+    }
+    
+    
+    /**
+     *  Loads the target folder initialize shown to the user.
+     * 
+     *  The target account has to be chosen before this method is called. 
+     */
+    private void initTargetFolder() {
+        if (mStorageManager == null) {
+            throw new IllegalStateException("Do not call this method before initializing mStorageManager");
+        }
+        
+        SharedPreferences appPreferences = PreferenceManager
+                .getDefaultSharedPreferences(getApplicationContext());
+
+        String last_path = appPreferences.getString("last_upload_path", "");
+        // "/" equals root-directory
+        if(last_path.equals("/")) {
+            mParents.add("");
+        }
+        else{
+            String[] dir_names = last_path.split("/");
+            for (String dir : dir_names)
+                mParents.add(dir);
+        }
+        //Make sure that path still exists, if it doesn't pop the stack and try the previous path
+            while(!mStorageManager.fileExists(generatePath(mParents)) && mParents.size() > 1){
+                mParents.pop();
+            }
+    }
 
+    
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        boolean retval = true;
+        switch (item.getItemId()) {
+        case android.R.id.home: {
+            if((mParents.size() > 1)) {                
+                onBackPressed(); 
+            }
+            break;
+        }
+        default:
+            retval = super.onOptionsItemSelected(item);
+        }
+        return retval;
+    }
+
+    
 }