Merge branch 'develop' into better_loggin_system
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / ExtendedListFragment.java
index d5c01b4..45a6aaf 100644 (file)
@@ -18,6 +18,8 @@
 
 package com.owncloud.android.ui.fragment;
 
+import java.util.ArrayList;
+
 import android.os.Bundle;
 import android.support.v4.widget.SwipeRefreshLayout;
 import android.view.LayoutInflater;
@@ -37,11 +39,17 @@ import com.owncloud.android.ui.ExtendedListView;
 /**
  *  TODO extending SherlockListFragment instead of SherlockFragment 
  */
-public class ExtendedListFragment extends SherlockFragment implements OnItemClickListener, SwipeRefreshLayout.OnRefreshListener{
+public class ExtendedListFragment extends SherlockFragment 
+implements OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {
     
     private static final String TAG = ExtendedListFragment.class.getSimpleName();
 
     private static final String KEY_SAVED_LIST_POSITION = "SAVED_LIST_POSITION"; 
+    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";
 
     protected ExtendedListView mList;
     
@@ -49,6 +57,15 @@ public class ExtendedListFragment extends SherlockFragment implements OnItemClic
     private SwipeRefreshLayout mRefreshEmptyLayout;
     private TextView mEmptyListMessage;
     
+    // Save the state of the scroll in browsing
+    private ArrayList<Integer> mIndexes;
+    private ArrayList<Integer> mFirstPositions;
+    private ArrayList<Integer> mTops;
+    private int mHeightCell = 0;
+
+    private SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = null;
+    
+    
     public void setListAdapter(ListAdapter listAdapter) {
         mList.setAdapter(listAdapter);
         mList.invalidate();
@@ -62,7 +79,6 @@ public class ExtendedListFragment extends SherlockFragment implements OnItemClic
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         Log_OC.e(TAG, "onCreateView");
-        //mList = new ExtendedListView(getActivity());
         
         View v = inflater.inflate(R.layout.list_fragment, null);
         mEmptyListMessage = (TextView) v.findViewById(R.id.empty_list_view);
@@ -90,11 +106,39 @@ public class ExtendedListFragment extends SherlockFragment implements OnItemClic
     }
 
     
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void onActivityCreated(Bundle savedInstanceState) {
+        super.onActivityCreated(savedInstanceState);
+        
+        if (savedInstanceState != null) {
+            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;
+        }
+    }    
+    
+    
     @Override
     public void onSaveInstanceState(Bundle savedInstanceState) {
         super.onSaveInstanceState(savedInstanceState);
         Log_OC.e(TAG, "onSaveInstanceState()");
         savedInstanceState.putInt(KEY_SAVED_LIST_POSITION, getReferencePosition());
+        savedInstanceState.putIntegerArrayList(KEY_INDEXES, mIndexes);
+        savedInstanceState.putIntegerArrayList(KEY_FIRST_POSITIONS, mFirstPositions);
+        savedInstanceState.putIntegerArrayList(KEY_TOPS, mTops);
+        savedInstanceState.putInt(KEY_HEIGHT_CELL, mHeightCell);
+        savedInstanceState.putString(KEY_EMPTY_LIST_MESSAGE, getEmptyViewText());
     }
 
     
@@ -126,6 +170,60 @@ public class ExtendedListFragment extends SherlockFragment implements OnItemClic
         }
     }
 
+
+    /*
+     * Restore index and position
+     */
+    protected 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
+     */
+    protected 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<?> parent, View view, int position, long id) {
         // to be @overriden  
@@ -136,7 +234,16 @@ public class ExtendedListFragment extends SherlockFragment implements OnItemClic
         // to be @overriden
         mRefreshLayout.setRefreshing(false);
         mRefreshEmptyLayout.setRefreshing(false);
+        
+        if (mOnRefreshListener != null) {
+            mOnRefreshListener.onRefresh();
+        }
+    }
+    
+    public void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener) {
+        mOnRefreshListener = listener;
     }
+    
 
     /**
      * Enables swipe gesture