1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2014 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/>.
18 package com
.owncloud
.android
.ui
.fragment
;
21 import java
.util
.ArrayList
;
23 import android
.app
.Activity
;
24 import android
.os
.Bundle
;
25 import android
.view
.View
;
26 import android
.widget
.AdapterView
;
28 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
29 import com
.owncloud
.android
.datamodel
.OCFile
;
30 import com
.owncloud
.android
.ui
.activity
.MoveActivity
;
31 import com
.owncloud
.android
.ui
.adapter
.FolderListListAdapter
;
32 import com
.owncloud
.android
.utils
.Log_OC
;
35 * A Fragment that lists all folders in a given path.
37 * TODO refactorize to get rid of direct dependency on MoveActivity
40 public class MoveFileListFragment
extends ExtendedListFragment
{
42 private static final String TAG
= MoveFileListFragment
.class.getSimpleName();
44 private static final String MY_PACKAGE
= MoveFileListFragment
.class.getPackage() != null ? MoveFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
45 private static final String EXTRA_FILE
= MY_PACKAGE
+ ".extra.FILE";
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 private FileFragment
.ContainerActivity mContainerActivity
;
55 private OCFile mFile
= null
;
56 private FolderListListAdapter mAdapter
;
58 // Save the state of the scroll in browsing
59 private ArrayList
<Integer
> mIndexes
;
60 private ArrayList
<Integer
> mFirstPositions
;
61 private ArrayList
<Integer
> mTops
;
63 private int mHeightCell
= 0;
69 public void onAttach(Activity activity
) {
70 super.onAttach(activity
);
71 Log_OC
.e(TAG
, "onAttach");
73 mContainerActivity
= (FileFragment
.ContainerActivity
) activity
;
74 } catch (ClassCastException e
) {
75 throw new ClassCastException(activity
.toString() + " must implement " +
76 FileFragment
.ContainerActivity
.class.getSimpleName());
82 public void onDetach() {
83 mContainerActivity
= null
;
91 public void onActivityCreated(Bundle savedInstanceState
) {
92 super.onActivityCreated(savedInstanceState
);
93 Log_OC
.e(TAG
, "onActivityCreated() start");
95 mAdapter
= new FolderListListAdapter(getSherlockActivity(), mContainerActivity
);
97 if (savedInstanceState
!= null
) {
98 mFile
= savedInstanceState
.getParcelable(EXTRA_FILE
);
99 mIndexes
= savedInstanceState
.getIntegerArrayList(KEY_INDEXES
);
100 mFirstPositions
= savedInstanceState
.getIntegerArrayList(KEY_FIRST_POSITIONS
);
101 mTops
= savedInstanceState
.getIntegerArrayList(KEY_TOPS
);
102 mHeightCell
= savedInstanceState
.getInt(KEY_HEIGHT_CELL
);
103 setMessageForEmptyList(savedInstanceState
.getString(KEY_EMPTY_LIST_MESSAGE
));
106 mIndexes
= new ArrayList
<Integer
>();
107 mFirstPositions
= new ArrayList
<Integer
>();
108 mTops
= new ArrayList
<Integer
>();
113 mAdapter
= new FolderListListAdapter(getSherlockActivity(), mContainerActivity
);
115 setListAdapter(mAdapter
);
117 registerForContextMenu(getListView());
118 getListView().setOnCreateContextMenuListener(this);
122 * Saves the current listed folder.
125 public void onSaveInstanceState (Bundle outState
) {
126 super.onSaveInstanceState(outState
);
127 outState
.putParcelable(EXTRA_FILE
, mFile
);
128 outState
.putIntegerArrayList(KEY_INDEXES
, mIndexes
);
129 outState
.putIntegerArrayList(KEY_FIRST_POSITIONS
, mFirstPositions
);
130 outState
.putIntegerArrayList(KEY_TOPS
, mTops
);
131 outState
.putInt(KEY_HEIGHT_CELL
, mHeightCell
);
132 outState
.putString(KEY_EMPTY_LIST_MESSAGE
, getEmptyViewText());
136 * Call this, when the user presses the up button.
138 * Tries to move up the current folder one level. If the parent folder was removed from the database,
139 * it continues browsing up until finding an existing folders.
141 * return Count of folder levels browsed up.
143 public int onBrowseUp() {
144 OCFile parentDir
= null
;
148 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
150 String parentPath
= null
;
151 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
152 parentPath
= new File(mFile
.getRemotePath()).getParent();
153 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
154 parentDir
= storageManager
.getFileByPath(parentPath
);
157 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
); // never returns null; keep the path in root folder
159 while (parentDir
== null
) {
160 parentPath
= new File(parentPath
).getParent();
161 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
162 parentDir
= storageManager
.getFileByPath(parentPath
);
164 } // exit is granted because storageManager.getFileByPath("/") never returns null
169 listDirectory(mFile
);
171 ((MoveActivity
)mContainerActivity
).startSyncFolderOperation(mFile
);
173 // restore index and top position
174 restoreIndexAndTopPosition();
176 } // else - should never happen now
182 * Restore index and position
184 private void restoreIndexAndTopPosition() {
185 if (mIndexes
.size() > 0) {
186 // needs to be checked; not every browse-up had a browse-down before
188 int index
= mIndexes
.remove(mIndexes
.size() - 1);
190 int firstPosition
= mFirstPositions
.remove(mFirstPositions
.size() -1);
192 int top
= mTops
.remove(mTops
.size() - 1);
194 mList
.setSelectionFromTop(firstPosition
, top
);
196 // Move the scroll if the selection is not visible
197 int indexPosition
= mHeightCell
*index
;
198 int height
= mList
.getHeight();
200 if (indexPosition
> height
) {
201 if (android
.os
.Build
.VERSION
.SDK_INT
>= 11)
203 mList
.smoothScrollToPosition(index
);
205 else if (android
.os
.Build
.VERSION
.SDK_INT
>= 8)
207 mList
.setSelectionFromTop(index
, 0);
215 * Save index and top position
217 private void saveIndexAndTopPosition(int index
) {
221 int firstPosition
= mList
.getFirstVisiblePosition();
222 mFirstPositions
.add(firstPosition
);
224 View view
= mList
.getChildAt(0);
225 int top
= (view
== null
) ?
0 : view
.getTop() ;
229 // Save the height of a cell
230 mHeightCell
= (view
== null
|| mHeightCell
!= 0) ? mHeightCell
: view
.getHeight();
234 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
235 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
237 if (file
.isFolder()) {
238 // update state and view of this fragment
240 // then, notify parent activity to let it update its state and view, and other fragments
241 mContainerActivity
.onBrowsedDownTo(file
);
242 // save index and top position
243 saveIndexAndTopPosition(position
);
248 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
254 * Use this to query the {@link OCFile} that is currently
255 * being displayed by this fragment
256 * @return The currently viewed OCFile
258 public OCFile
getCurrentFile(){
263 * Calls {@link MoveFileListFragment#listDirectory(OCFile)} with a null parameter
265 public void listDirectory(){
270 * Lists the given directory on the view. When the input parameter is null,
271 * it will either refresh the last known directory. list the root
272 * if there never was a directory.
274 * @param directory File to be listed
276 public void listDirectory(OCFile directory
) {
277 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
278 if (storageManager
!= null
) {
280 // Check input parameters for null
281 if(directory
== null
){
285 directory
= storageManager
.getFileByPath("/");
286 if (directory
== null
) return; // no files, wait for sync
291 // If that's not a directory -> List its parent
292 if(!directory
.isFolder()){
293 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
294 directory
= storageManager
.getFileById(directory
.getParentId());
297 mAdapter
.swapDirectory(directory
, storageManager
);
298 if (mFile
== null
|| !mFile
.equals(directory
)) {
299 mList
.setSelectionFromTop(0, 0);
307 public void onRefresh() {
312 mFile
= mContainerActivity
.getStorageManager().getFileById(mFile
.getFileId());
314 listDirectory(mFile
);
316 ((MoveActivity
)mContainerActivity
).startSyncFolderOperation(mFile
);