Merge branch 'develop' into better_loggin_system
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / OCFileListFragment.java
index 85838e6..099e442 100644 (file)
 package com.owncloud.android.ui.fragment;
 
 import java.io.File;
-import java.util.ArrayList;
 
 import android.app.Activity;
+import android.content.Intent;
 import android.os.Bundle;
+import android.support.v4.widget.SwipeRefreshLayout;
 import android.view.ContextMenu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
@@ -35,6 +36,7 @@ import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.FileMenuFilter;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.ui.activity.FileDisplayActivity;
+import com.owncloud.android.ui.activity.MoveActivity;
 import com.owncloud.android.ui.adapter.FileListListAdapter;
 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
 import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
@@ -55,15 +57,14 @@ public class OCFileListFragment extends ExtendedListFragment {
     
     private static final String TAG = OCFileListFragment.class.getSimpleName();
 
-    private static final String MY_PACKAGE = OCFileListFragment.class.getPackage() != null ? OCFileListFragment.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
-    private static final String EXTRA_FILE = MY_PACKAGE + ".extra.FILE";
+    private static final String MY_PACKAGE = OCFileListFragment.class.getPackage() != null ?
+            OCFileListFragment.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
+            
+    public final static String ARG_JUST_FOLDERS = MY_PACKAGE + ".JUST_FOLDERS";
+    public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS = MY_PACKAGE + ".ALLOW_CONTEXTUAL";
+            
+    private static final String KEY_FILE = MY_PACKAGE + ".extra.FILE";
 
-    private static final String KEY_INDEXES = "INDEXES";
-    private static final String KEY_FIRST_POSITIONS= "FIRST_POSITIONS";
-    private static final String KEY_TOPS = "TOPS";
-    private static final String KEY_HEIGHT_CELL = "HEIGHT_CELL";
-    private static final String KEY_EMPTY_LIST_MESSAGE = "EMPTY_LIST_MESSAGE";
-    
     private FileFragment.ContainerActivity mContainerActivity;
    
     private OCFile mFile = null;
@@ -71,12 +72,6 @@ public class OCFileListFragment extends ExtendedListFragment {
     
     private OCFile mTargetFile;
 
-    // Save the state of the scroll in browsing
-    private ArrayList<Integer> mIndexes;
-    private ArrayList<Integer> mFirstPositions;
-    private ArrayList<Integer> mTops;
-
-    private int mHeightCell = 0;
     
     /**
      * {@inheritDoc}
@@ -87,15 +82,24 @@ public class OCFileListFragment extends ExtendedListFragment {
         Log_OC.e(TAG, "onAttach");
         try {
             mContainerActivity = (FileFragment.ContainerActivity) activity;
+            
         } catch (ClassCastException e) {
             throw new ClassCastException(activity.toString() + " must implement " + 
                     FileFragment.ContainerActivity.class.getSimpleName());
         }
+        try {
+            setOnRefreshListener((SwipeRefreshLayout.OnRefreshListener) activity);
+            
+        } catch (ClassCastException e) {
+            throw new ClassCastException(activity.toString() + " must implement " + 
+                    SwipeRefreshLayout.OnRefreshListener.class.getSimpleName());
+        }
     }
 
     
     @Override
     public void onDetach() {
+        setOnRefreshListener(null);
         mContainerActivity = null;
         super.onDetach();
     }
@@ -108,26 +112,17 @@ public class OCFileListFragment extends ExtendedListFragment {
         super.onActivityCreated(savedInstanceState);
         Log_OC.e(TAG, "onActivityCreated() start");
         
-        mAdapter = new FileListListAdapter(getSherlockActivity(), mContainerActivity); 
-                
         if (savedInstanceState != null) {
-            mFile = savedInstanceState.getParcelable(EXTRA_FILE);
-            mIndexes = savedInstanceState.getIntegerArrayList(KEY_INDEXES);
-            mFirstPositions = savedInstanceState.getIntegerArrayList(KEY_FIRST_POSITIONS);
-            mTops = savedInstanceState.getIntegerArrayList(KEY_TOPS);
-            mHeightCell = savedInstanceState.getInt(KEY_HEIGHT_CELL);
-            setMessageForEmptyList(savedInstanceState.getString(KEY_EMPTY_LIST_MESSAGE));
-            
-        } else {
-            mIndexes = new ArrayList<Integer>();
-            mFirstPositions = new ArrayList<Integer>();
-            mTops = new ArrayList<Integer>();
-            mHeightCell = 0;
-            
+            mFile = savedInstanceState.getParcelable(KEY_FILE);
         }
         
-        mAdapter = new FileListListAdapter(getSherlockActivity(), mContainerActivity);
-        
+        Bundle args = getArguments();
+        boolean justFolders = (args == null) ? false : args.getBoolean(ARG_JUST_FOLDERS, false); 
+        mAdapter = new FileListListAdapter(
+                justFolders,
+                getSherlockActivity(), 
+                mContainerActivity
+        );
         setListAdapter(mAdapter);
         
         registerForContextMenu(getListView());
@@ -140,19 +135,14 @@ public class OCFileListFragment extends ExtendedListFragment {
     @Override
     public void onSaveInstanceState (Bundle outState) {
         super.onSaveInstanceState(outState);
-        outState.putParcelable(EXTRA_FILE, mFile);
-        outState.putIntegerArrayList(KEY_INDEXES, mIndexes);
-        outState.putIntegerArrayList(KEY_FIRST_POSITIONS, mFirstPositions);
-        outState.putIntegerArrayList(KEY_TOPS, mTops);
-        outState.putInt(KEY_HEIGHT_CELL, mHeightCell);
-        outState.putString(KEY_EMPTY_LIST_MESSAGE, getEmptyViewText());
+        outState.putParcelable(KEY_FILE, mFile);
     }
     
     /**
      * Call this, when the user presses the up button.
      * 
-     * Tries to move up the current folder one level. If the parent folder was removed from the database, 
-     * it continues browsing up until finding an existing folders.
+     * Tries to move up the current folder one level. If the parent folder was removed from the 
+     * database, it continues browsing up until finding an existing folders.
      * 
      * return       Count of folder levels browsed up.
      */
@@ -166,25 +156,25 @@ public class OCFileListFragment extends ExtendedListFragment {
             String parentPath = null;
             if (mFile.getParentId() != FileDataStorageManager.ROOT_PARENT_ID) {
                 parentPath = new File(mFile.getRemotePath()).getParent();
-                parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
+                parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : 
+                       parentPath + OCFile.PATH_SEPARATOR;
                 parentDir = storageManager.getFileByPath(parentPath);
                 moveCount++;
             } else {
-                parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH);    // never returns null; keep the path in root folder
+                parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH);
             }
             while (parentDir == null) {
                 parentPath = new File(parentPath).getParent();
-                parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
+                parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : 
+                       parentPath + OCFile.PATH_SEPARATOR;
                 parentDir = storageManager.getFileByPath(parentPath);
                 moveCount++;
             }   // exit is granted because storageManager.getFileByPath("/") never returns null
-            mFile = parentDir;           
-        }
-        
-        if (mFile != null) {
+            mFile = parentDir;
+            
             listDirectory(mFile);
 
-            ((FileDisplayActivity)mContainerActivity).startSyncFolderOperation(mFile);
+            onRefresh();
             
             // restore index and top position
             restoreIndexAndTopPosition();
@@ -194,58 +184,6 @@ public class OCFileListFragment extends ExtendedListFragment {
         return moveCount;
     }
     
-    /*
-     * Restore index and position
-     */
-    private void restoreIndexAndTopPosition() {
-        if (mIndexes.size() > 0) {  
-            // needs to be checked; not every browse-up had a browse-down before 
-            
-            int index = mIndexes.remove(mIndexes.size() - 1);
-            
-            int firstPosition = mFirstPositions.remove(mFirstPositions.size() -1);
-            
-            int top = mTops.remove(mTops.size() - 1);
-            
-            mList.setSelectionFromTop(firstPosition, top);
-            
-            // Move the scroll if the selection is not visible
-            int indexPosition = mHeightCell*index;
-            int height = mList.getHeight();
-            
-            if (indexPosition > height) {
-                if (android.os.Build.VERSION.SDK_INT >= 11)
-                {
-                    mList.smoothScrollToPosition(index); 
-                }
-                else if (android.os.Build.VERSION.SDK_INT >= 8)
-                {
-                    mList.setSelectionFromTop(index, 0);
-                }
-                
-            }
-        }
-    }
-    
-    /*
-     * Save index and top position
-     */
-    private void saveIndexAndTopPosition(int index) {
-        
-        mIndexes.add(index);
-        
-        int firstPosition = mList.getFirstVisiblePosition();
-        mFirstPositions.add(firstPosition);
-        
-        View view = mList.getChildAt(0);
-        int top = (view == null) ? 0 : view.getTop() ;
-
-        mTops.add(top);
-        
-        // Save the height of a cell
-        mHeightCell = (view == null || mHeightCell != 0) ? mHeightCell : view.getHeight();
-    }
-    
     @Override
     public void onItemClick(AdapterView<?> l, View v, int position, long id) {
         OCFile file = (OCFile) mAdapter.getItem(position);
@@ -253,7 +191,7 @@ public class OCFileListFragment extends ExtendedListFragment {
             if (file.isFolder()) { 
                 // update state and view of this fragment
                 listDirectory(file);
-                // then, notify parent activity to let it update its state and view, and other fragments
+                // then, notify parent activity to let it update its state and view
                 mContainerActivity.onBrowsedDownTo(file);
                 // save index and top position
                 saveIndexAndTopPosition(position);
@@ -288,42 +226,46 @@ public class OCFileListFragment extends ExtendedListFragment {
      * {@inheritDoc}
      */
     @Override
-    public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
+    public void onCreateContextMenu (
+            ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
         super.onCreateContextMenu(menu, v, menuInfo);
-        MenuInflater inflater = getSherlockActivity().getMenuInflater();
-        inflater.inflate(R.menu.file_actions_menu, menu);
-        AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
-        OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
-        
-        if (mContainerActivity.getStorageManager() != null) {
-            FileMenuFilter mf = new FileMenuFilter(
-                targetFile,
-                mContainerActivity.getStorageManager().getAccount(),
-                mContainerActivity,
-                getSherlockActivity()
-            );
-            mf.filter(menu);
-        }
-        
-        /// additional restrictions for this fragment 
-        // TODO allow in the future 'open with' for previewable files
-        MenuItem item = menu.findItem(R.id.action_open_file_with);
-        if (item != null) {
-            item.setVisible(false);
-            item.setEnabled(false);
-        }
-        /// TODO break this direct dependency on FileDisplayActivity... if possible
-        FileFragment frag = ((FileDisplayActivity)getSherlockActivity()).getSecondFragment();
-        if (frag != null && frag instanceof FileDetailFragment && 
-                frag.getFile().getFileId() == targetFile.getFileId()) {
-            item = menu.findItem(R.id.action_see_details);
+        Bundle args = getArguments();
+        boolean allowContextualActions = 
+                (args == null) ? true : args.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS, true); 
+        if (allowContextualActions) {
+            MenuInflater inflater = getSherlockActivity().getMenuInflater();
+            inflater.inflate(R.menu.file_actions_menu, menu);
+            AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
+            OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
+            
+            if (mContainerActivity.getStorageManager() != null) {
+                FileMenuFilter mf = new FileMenuFilter(
+                    targetFile,
+                    mContainerActivity.getStorageManager().getAccount(),
+                    mContainerActivity,
+                    getSherlockActivity()
+                );
+                mf.filter(menu);
+            }
+            
+            /// additional restrictions for this fragment 
+            // TODO allow in the future 'open with' for previewable files
+            MenuItem item = menu.findItem(R.id.action_open_file_with);
             if (item != null) {
                 item.setVisible(false);
                 item.setEnabled(false);
             }
+            /// TODO break this direct dependency on FileDisplayActivity... if possible
+            FileFragment frag = ((FileDisplayActivity)getSherlockActivity()).getSecondFragment();
+            if (frag != null && frag instanceof FileDetailFragment && 
+                    frag.getFile().getFileId() == targetFile.getFileId()) {
+                item = menu.findItem(R.id.action_see_details);
+                if (item != null) {
+                    item.setVisible(false);
+                    item.setEnabled(false);
+                }
+            }
         }
-        
-
     }
     
     
@@ -378,6 +320,14 @@ public class OCFileListFragment extends ExtendedListFragment {
                 }
                 return true;
             }
+            case R.id.action_move: {
+                Intent action = new Intent(getActivity(), MoveActivity.class);
+
+                // Pass mTargetFile that contains info of selected file/folder
+                action.putExtra(MoveActivity.EXTRA_TARGET_FILE, mTargetFile);
+                getActivity().startActivityForResult(action, FileDisplayActivity.ACTION_MOVE_FILES);
+                return true;
+            }
             default:
                 return super.onContextItemSelected(item); 
         }
@@ -436,21 +386,4 @@ public class OCFileListFragment extends ExtendedListFragment {
         }
     }
 
-
-    @Override
-    public void onRefresh() {
-        super.onRefresh();
-        
-        if (mFile != null) {
-            // Refresh mFile
-            mFile = mContainerActivity.getStorageManager().getFileById(mFile.getFileId());
-
-            listDirectory(mFile);
-            
-            ((FileDisplayActivity)mContainerActivity).startSyncFolderOperation(mFile);
-        }
-    }
-    
-    
-    
 }