1 /* ownCloud Android client application 
   2  *   Copyright (C) 2012 Bartek Przybylski 
   3  *   Copyright (C) 2012-2013 ownCloud Inc. 
   5  *   This program is free software: you can redistribute it and/or modify 
   6  *   it under the terms of the GNU General Public License version 2, 
   7  *   as published by the Free Software Foundation. 
   9  *   This program is distributed in the hope that it will be useful, 
  10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  12  *   GNU General Public License for more details. 
  14  *   You should have received a copy of the GNU General Public License 
  15  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  19 package com
.owncloud
.android
.ui
.fragment
; 
  21 import java
.util
.ArrayList
; 
  23 import android
.os
.Bundle
; 
  24 import android
.support
.v4
.widget
.SwipeRefreshLayout
; 
  25 import android
.view
.LayoutInflater
; 
  26 import android
.view
.View
; 
  27 import android
.view
.ViewGroup
; 
  28 import android
.widget
.AdapterView
; 
  29 import android
.widget
.AdapterView
.OnItemClickListener
; 
  30 import android
.widget
.ListAdapter
; 
  31 import android
.widget
.ListView
; 
  32 import android
.widget
.TextView
; 
  34 import com
.actionbarsherlock
.app
.SherlockFragment
; 
  35 import com
.owncloud
.android
.R
; 
  36 import com
.owncloud
.android
.ui
.ExtendedListView
; 
  37 import com
.owncloud
.android
.utils
.Log_OC
; 
  40  *  TODO extending SherlockListFragment instead of SherlockFragment  
  42 public class ExtendedListFragment 
extends SherlockFragment 
implements OnItemClickListener
, SwipeRefreshLayout
.OnRefreshListener
{ 
  44     private static final String TAG 
= ExtendedListFragment
.class.getSimpleName(); 
  46     private static final String KEY_SAVED_LIST_POSITION 
= "SAVED_LIST_POSITION";  
  47     private static final String KEY_INDEXES 
= "INDEXES"; 
  48     private static final String KEY_FIRST_POSITIONS
= "FIRST_POSITIONS"; 
  49     private static final String KEY_TOPS 
= "TOPS"; 
  50     private static final String KEY_HEIGHT_CELL 
= "HEIGHT_CELL"; 
  51     private static final String KEY_EMPTY_LIST_MESSAGE 
= "EMPTY_LIST_MESSAGE"; 
  53     protected ExtendedListView mList
; 
  55     private SwipeRefreshLayout mRefreshLayout
; 
  56     private SwipeRefreshLayout mRefreshEmptyLayout
; 
  57     private TextView mEmptyListMessage
; 
  59     // Save the state of the scroll in browsing 
  60     private ArrayList
<Integer
> mIndexes
; 
  61     private ArrayList
<Integer
> mFirstPositions
; 
  62     private ArrayList
<Integer
> mTops
; 
  63     private int mHeightCell 
= 0; 
  66     public void setListAdapter(ListAdapter listAdapter
) { 
  67         mList
.setAdapter(listAdapter
); 
  71     public ListView 
getListView() { 
  77     public View 
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) { 
  78         Log_OC
.e(TAG
, "onCreateView"); 
  80         View v 
= inflater
.inflate(R
.layout
.list_fragment
, null
); 
  81         mEmptyListMessage 
= (TextView
) v
.findViewById(R
.id
.empty_list_view
); 
  82         mList 
= (ExtendedListView
)(v
.findViewById(R
.id
.list_root
)); 
  83         mList
.setOnItemClickListener(this); 
  85         mList
.setDivider(getResources().getDrawable(R
.drawable
.uploader_list_separator
)); 
  86         mList
.setDividerHeight(1); 
  88         if (savedInstanceState 
!= null
) { 
  89             int referencePosition 
= savedInstanceState
.getInt(KEY_SAVED_LIST_POSITION
); 
  90             setReferencePosition(referencePosition
); 
  94         mRefreshLayout 
= (SwipeRefreshLayout
) v
.findViewById(R
.id
.swipe_refresh_files
); 
  95         mRefreshEmptyLayout 
= (SwipeRefreshLayout
) v
.findViewById(R
.id
.swipe_refresh_files_emptyView
); 
  97         onCreateSwipeToRefresh(mRefreshLayout
); 
  98         onCreateSwipeToRefresh(mRefreshEmptyLayout
); 
 100         mList
.setEmptyView(mRefreshEmptyLayout
); 
 110     public void onActivityCreated(Bundle savedInstanceState
) { 
 111         super.onActivityCreated(savedInstanceState
); 
 113         if (savedInstanceState 
!= null
) { 
 114             mIndexes 
= savedInstanceState
.getIntegerArrayList(KEY_INDEXES
); 
 115             mFirstPositions 
= savedInstanceState
.getIntegerArrayList(KEY_FIRST_POSITIONS
); 
 116             mTops 
= savedInstanceState
.getIntegerArrayList(KEY_TOPS
); 
 117             mHeightCell 
= savedInstanceState
.getInt(KEY_HEIGHT_CELL
); 
 118             setMessageForEmptyList(savedInstanceState
.getString(KEY_EMPTY_LIST_MESSAGE
)); 
 121             mIndexes 
= new ArrayList
<Integer
>(); 
 122             mFirstPositions 
= new ArrayList
<Integer
>(); 
 123             mTops 
= new ArrayList
<Integer
>(); 
 130     public void onSaveInstanceState(Bundle savedInstanceState
) { 
 131         super.onSaveInstanceState(savedInstanceState
); 
 132         Log_OC
.e(TAG
, "onSaveInstanceState()"); 
 133         savedInstanceState
.putInt(KEY_SAVED_LIST_POSITION
, getReferencePosition()); 
 134         savedInstanceState
.putIntegerArrayList(KEY_INDEXES
, mIndexes
); 
 135         savedInstanceState
.putIntegerArrayList(KEY_FIRST_POSITIONS
, mFirstPositions
); 
 136         savedInstanceState
.putIntegerArrayList(KEY_TOPS
, mTops
); 
 137         savedInstanceState
.putInt(KEY_HEIGHT_CELL
, mHeightCell
); 
 138         savedInstanceState
.putString(KEY_EMPTY_LIST_MESSAGE
, getEmptyViewText()); 
 143      * Calculates the position of the item that will be used as a reference to reposition the visible items in the list when 
 144      * the device is turned to other position.  
 146      * THe current policy is take as a reference the visible item in the center of the screen.   
 148      * @return      The position in the list of the visible item in the center of the screen. 
 150     protected int getReferencePosition() { 
 152             return (mList
.getFirstVisiblePosition() + mList
.getLastVisiblePosition()) / 2; 
 160      * Sets the visible part of the list from the reference position. 
 162      * @param   position    Reference position previously returned by {@link LocalFileListFragment#getReferencePosition()} 
 164     protected void setReferencePosition(int position
) { 
 166             mList
.setAndCenterSelection(position
); 
 172      * Restore index and position 
 174     protected void restoreIndexAndTopPosition() { 
 175         if (mIndexes
.size() > 0) {   
 176             // needs to be checked; not every browse-up had a browse-down before  
 178             int index 
= mIndexes
.remove(mIndexes
.size() - 1); 
 180             int firstPosition 
= mFirstPositions
.remove(mFirstPositions
.size() -1); 
 182             int top 
= mTops
.remove(mTops
.size() - 1); 
 184             mList
.setSelectionFromTop(firstPosition
, top
); 
 186             // Move the scroll if the selection is not visible 
 187             int indexPosition 
= mHeightCell
*index
; 
 188             int height 
= mList
.getHeight(); 
 190             if (indexPosition 
> height
) { 
 191                 if (android
.os
.Build
.VERSION
.SDK_INT 
>= 11) 
 193                     mList
.smoothScrollToPosition(index
);  
 195                 else if (android
.os
.Build
.VERSION
.SDK_INT 
>= 8) 
 197                     mList
.setSelectionFromTop(index
, 0); 
 205      * Save index and top position 
 207     protected void saveIndexAndTopPosition(int index
) { 
 211         int firstPosition 
= mList
.getFirstVisiblePosition(); 
 212         mFirstPositions
.add(firstPosition
); 
 214         View view 
= mList
.getChildAt(0); 
 215         int top 
= (view 
== null
) ? 
0 : view
.getTop() ; 
 219         // Save the height of a cell 
 220         mHeightCell 
= (view 
== null 
|| mHeightCell 
!= 0) ? mHeightCell 
: view
.getHeight(); 
 225     public void onItemClick (AdapterView
<?
> parent
, View view
, int position
, long id
) { 
 230     public void onRefresh() { 
 232         mRefreshLayout
.setRefreshing(false
); 
 233         mRefreshEmptyLayout
.setRefreshing(false
); 
 237      * Enables swipe gesture 
 239     public void enableSwipe() { 
 240         mRefreshLayout
.setEnabled(true
); 
 244      * Disables swipe gesture. It prevents manual gestures but keeps the option you show 
 245      * refreshing programmatically. 
 247     public void disableSwipe() { 
 248         mRefreshLayout
.setEnabled(false
); 
 252      * It shows the SwipeRefreshLayout progress 
 254     public void showSwipeProgress() { 
 255         mRefreshLayout
.setRefreshing(true
); 
 259      * It shows the SwipeRefreshLayout progress 
 261     public void hideSwipeProgress() { 
 262         mRefreshLayout
.setRefreshing(false
); 
 266      * Set message for empty list view 
 268     public void setMessageForEmptyList(String message
) { 
 269         if (mEmptyListMessage 
!= null
) { 
 270             mEmptyListMessage
.setText(message
); 
 275      * Get the text of EmptyListMessage TextView 
 279     public String 
getEmptyViewText() { 
 280         return (mEmptyListMessage 
!= null
) ? mEmptyListMessage
.getText().toString() : ""; 
 283     private void onCreateSwipeToRefresh(SwipeRefreshLayout refreshLayout
) { 
 284         // Colors in animations: background 
 285         refreshLayout
.setColorScheme(R
.color
.background_color
, R
.color
.background_color
, R
.color
.background_color
, 
 286                 R
.color
.background_color
); 
 288         refreshLayout
.setOnRefreshListener(this);