Some clean-up and refactoring in fragments listing files
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / MoveFileListFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.ui.fragment;
19
20 import java.io.File;
21
22 import android.app.Activity;
23 import android.os.Bundle;
24 import android.view.View;
25 import android.widget.AdapterView;
26
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;
32
33 /**
34 * A Fragment that shows all the folders in a given path, and allows browsing through them.
35 *
36 * TODO refactorize to get rid of direct dependency on MoveActivity
37 */
38 public class MoveFileListFragment extends ExtendedListFragment {
39
40 private static final String TAG = MoveFileListFragment.class.getSimpleName();
41
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";
45
46 private FileFragment.ContainerActivity mContainerActivity;
47
48 private OCFile mFile = null;
49 private FolderListListAdapter mAdapter;
50
51
52 /**
53 * {@inheritDoc}
54 */
55 @Override
56 public void onAttach(Activity activity) {
57 super.onAttach(activity);
58 Log_OC.e(TAG, "onAttach");
59 try {
60 mContainerActivity = (FileFragment.ContainerActivity) activity;
61 } catch (ClassCastException e) {
62 throw new ClassCastException(activity.toString() + " must implement " +
63 FileFragment.ContainerActivity.class.getSimpleName());
64 }
65 }
66
67
68 @Override
69 public void onDetach() {
70 mContainerActivity = null;
71 super.onDetach();
72 }
73
74 /**
75 * {@inheritDoc}
76 */
77 @Override
78 public void onActivityCreated(Bundle savedInstanceState) {
79 super.onActivityCreated(savedInstanceState);
80 Log_OC.e(TAG, "onActivityCreated() start");
81
82 if (savedInstanceState != null) {
83 mFile = savedInstanceState.getParcelable(EXTRA_FILE);
84 }
85
86 mAdapter = new FolderListListAdapter(getSherlockActivity(), mContainerActivity);
87 setListAdapter(mAdapter);
88
89 registerForContextMenu(getListView());
90 getListView().setOnCreateContextMenuListener(this);
91 }
92
93
94 /**
95 * Saves the current listed folder.
96 */
97 @Override
98 public void onSaveInstanceState (Bundle outState) {
99 super.onSaveInstanceState(outState);
100 outState.putParcelable(EXTRA_FILE, mFile);
101 }
102
103 /**
104 * Call this, when the user presses the up button.
105 *
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.
108 *
109 * return Count of folder levels browsed up.
110 */
111 public int onBrowseUp() {
112 OCFile parentDir = null;
113 int moveCount = 0;
114
115 if(mFile != null){
116 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
117
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);
124 moveCount++;
125 } else {
126 parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH);
127 }
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);
133 moveCount++;
134 } // exit is granted because storageManager.getFileByPath("/") never returns null
135 mFile = parentDir;
136
137 listDirectory(mFile);
138
139 ((MoveActivity)mContainerActivity).startSyncFolderOperation(mFile);
140
141 // restore index and top position
142 restoreIndexAndTopPosition();
143
144 } // else - should never happen now
145
146 return moveCount;
147 }
148
149 @Override
150 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
151 OCFile file = (OCFile) mAdapter.getItem(position);
152 if (file != null) {
153 if (file.isFolder()) {
154 // update state and view of this fragment
155 listDirectory(file);
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);
160
161 }
162
163 } else {
164 Log_OC.d(TAG, "Null object in ListAdapter!!");
165 }
166
167 }
168
169 /**
170 * Use this to query the {@link OCFile} that is currently
171 * being displayed by this fragment
172 * @return The currently viewed OCFile
173 */
174 public OCFile getCurrentFile(){
175 return mFile;
176 }
177
178 /**
179 * Calls {@link MoveFileListFragment#listDirectory(OCFile)} with a null parameter
180 */
181 public void listDirectory(){
182 listDirectory(null);
183 }
184
185 /**
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.
189 *
190 * @param directory File to be listed
191 */
192 public void listDirectory(OCFile directory) {
193 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
194 if (storageManager != null) {
195
196 // Check input parameters for null
197 if(directory == null){
198 if(mFile != null){
199 directory = mFile;
200 } else {
201 directory = storageManager.getFileByPath("/");
202 if (directory == null) return; // no files, wait for sync
203 }
204 }
205
206
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());
211 }
212
213 mAdapter.swapDirectory(directory, storageManager);
214 if (mFile == null || !mFile.equals(directory)) {
215 mList.setSelectionFromTop(0, 0);
216 }
217 mFile = directory;
218 }
219 }
220
221
222 @Override
223 public void onRefresh() {
224 super.onRefresh();
225
226 if (mFile != null) {
227 // Refresh mFile
228 mFile = mContainerActivity.getStorageManager().getFileById(mFile.getFileId());
229
230 listDirectory(mFile);
231
232 ((MoveActivity)mContainerActivity).startSyncFolderOperation(mFile);
233 }
234 }
235 }