1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.fragment
;
22 import android
.app
.Activity
;
23 import android
.os
.Bundle
;
24 import android
.view
.View
;
25 import android
.widget
.AdapterView
;
27 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
28 import com
.owncloud
.android
.datamodel
.OCFile
;
29 import com
.owncloud
.android
.ui
.activity
.MoveActivity
;
30 import com
.owncloud
.android
.ui
.adapter
.FolderListListAdapter
;
31 import com
.owncloud
.android
.utils
.Log_OC
;
34 * A Fragment that shows all the folders in a given path, and allows browsing through them.
36 * TODO refactorize to get rid of direct dependency on MoveActivity
38 public class MoveFileListFragment
extends ExtendedListFragment
{
40 private static final String TAG
= MoveFileListFragment
.class.getSimpleName();
42 private static final String MY_PACKAGE
= MoveFileListFragment
.class.getPackage() != null ?
43 MoveFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
44 private static final String EXTRA_FILE
= MY_PACKAGE
+ ".extra.FILE";
46 private FileFragment
.ContainerActivity mContainerActivity
;
48 private OCFile mFile
= null
;
49 private FolderListListAdapter mAdapter
;
56 public void onAttach(Activity activity
) {
57 super.onAttach(activity
);
58 Log_OC
.e(TAG
, "onAttach");
60 mContainerActivity
= (FileFragment
.ContainerActivity
) activity
;
61 } catch (ClassCastException e
) {
62 throw new ClassCastException(activity
.toString() + " must implement " +
63 FileFragment
.ContainerActivity
.class.getSimpleName());
69 public void onDetach() {
70 mContainerActivity
= null
;
78 public void onActivityCreated(Bundle savedInstanceState
) {
79 super.onActivityCreated(savedInstanceState
);
80 Log_OC
.e(TAG
, "onActivityCreated() start");
82 if (savedInstanceState
!= null
) {
83 mFile
= savedInstanceState
.getParcelable(EXTRA_FILE
);
86 mAdapter
= new FolderListListAdapter(getSherlockActivity(), mContainerActivity
);
87 setListAdapter(mAdapter
);
89 registerForContextMenu(getListView());
90 getListView().setOnCreateContextMenuListener(this);
95 * Saves the current listed folder.
98 public void onSaveInstanceState (Bundle outState
) {
99 super.onSaveInstanceState(outState
);
100 outState
.putParcelable(EXTRA_FILE
, mFile
);
104 * Call this, when the user presses the up button.
106 * Tries to move up the current folder one level. If the parent folder was removed from the
107 * database, it continues browsing up until finding an existing folders.
109 * return Count of folder levels browsed up.
111 public int onBrowseUp() {
112 OCFile parentDir
= null
;
116 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
118 String parentPath
= null
;
119 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
120 parentPath
= new File(mFile
.getRemotePath()).getParent();
121 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
122 parentPath
+ OCFile
.PATH_SEPARATOR
;
123 parentDir
= storageManager
.getFileByPath(parentPath
);
126 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
);
128 while (parentDir
== null
) {
129 parentPath
= new File(parentPath
).getParent();
130 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
131 parentPath
+ OCFile
.PATH_SEPARATOR
;
132 parentDir
= storageManager
.getFileByPath(parentPath
);
134 } // exit is granted because storageManager.getFileByPath("/") never returns null
137 listDirectory(mFile
);
139 ((MoveActivity
)mContainerActivity
).startSyncFolderOperation(mFile
);
141 // restore index and top position
142 restoreIndexAndTopPosition();
144 } // else - should never happen now
150 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
151 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
153 if (file
.isFolder()) {
154 // update state and view of this fragment
156 // then, notify parent activity to let it update its state and view
157 mContainerActivity
.onBrowsedDownTo(file
);
158 // save index and top position
159 saveIndexAndTopPosition(position
);
164 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
170 * Use this to query the {@link OCFile} that is currently
171 * being displayed by this fragment
172 * @return The currently viewed OCFile
174 public OCFile
getCurrentFile(){
179 * Calls {@link MoveFileListFragment#listDirectory(OCFile)} with a null parameter
181 public void listDirectory(){
186 * Lists the given directory on the view. When the input parameter is null,
187 * it will either refresh the last known directory. list the root
188 * if there never was a directory.
190 * @param directory File to be listed
192 public void listDirectory(OCFile directory
) {
193 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
194 if (storageManager
!= null
) {
196 // Check input parameters for null
197 if(directory
== null
){
201 directory
= storageManager
.getFileByPath("/");
202 if (directory
== null
) return; // no files, wait for sync
207 // If that's not a directory -> List its parent
208 if(!directory
.isFolder()){
209 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
210 directory
= storageManager
.getFileById(directory
.getParentId());
213 mAdapter
.swapDirectory(directory
, storageManager
);
214 if (mFile
== null
|| !mFile
.equals(directory
)) {
215 mList
.setSelectionFromTop(0, 0);
223 public void onRefresh() {
228 mFile
= mContainerActivity
.getStorageManager().getFileById(mFile
.getFileId());
230 listDirectory(mFile
);
232 ((MoveActivity
)mContainerActivity
).startSyncFolderOperation(mFile
);