Add new activity for selecting the destination folder when moving files, including...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / MoveFileListFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2014 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18 package com.owncloud.android.ui.fragment;
19
20 import java.io.File;
21 import java.util.ArrayList;
22
23 import android.app.Activity;
24 import android.os.Bundle;
25 import android.view.View;
26 import android.widget.AdapterView;
27
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;
33
34 /**
35 * A Fragment that lists all folders in a given path.
36 *
37 * TODO refactorize to get rid of direct dependency on MoveActivity
38 *
39 */
40 public class MoveFileListFragment extends ExtendedListFragment {
41
42 private static final String TAG = MoveFileListFragment.class.getSimpleName();
43
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";
46
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";
52
53 private FileFragment.ContainerActivity mContainerActivity;
54
55 private OCFile mFile = null;
56 private FolderListListAdapter mAdapter;
57
58 // Save the state of the scroll in browsing
59 private ArrayList<Integer> mIndexes;
60 private ArrayList<Integer> mFirstPositions;
61 private ArrayList<Integer> mTops;
62
63 private int mHeightCell = 0;
64
65 /**
66 * {@inheritDoc}
67 */
68 @Override
69 public void onAttach(Activity activity) {
70 super.onAttach(activity);
71 Log_OC.e(TAG, "onAttach");
72 try {
73 mContainerActivity = (FileFragment.ContainerActivity) activity;
74 } catch (ClassCastException e) {
75 throw new ClassCastException(activity.toString() + " must implement " +
76 FileFragment.ContainerActivity.class.getSimpleName());
77 }
78 }
79
80
81 @Override
82 public void onDetach() {
83 mContainerActivity = null;
84 super.onDetach();
85 }
86
87 /**
88 * {@inheritDoc}
89 */
90 @Override
91 public void onActivityCreated(Bundle savedInstanceState) {
92 super.onActivityCreated(savedInstanceState);
93 Log_OC.e(TAG, "onActivityCreated() start");
94
95 mAdapter = new FolderListListAdapter(getSherlockActivity(), mContainerActivity);
96
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));
104
105 } else {
106 mIndexes = new ArrayList<Integer>();
107 mFirstPositions = new ArrayList<Integer>();
108 mTops = new ArrayList<Integer>();
109 mHeightCell = 0;
110
111 }
112
113 mAdapter = new FolderListListAdapter(getSherlockActivity(), mContainerActivity);
114
115 setListAdapter(mAdapter);
116
117 registerForContextMenu(getListView());
118 getListView().setOnCreateContextMenuListener(this);
119 }
120
121 /**
122 * Saves the current listed folder.
123 */
124 @Override
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());
133 }
134
135 /**
136 * Call this, when the user presses the up button.
137 *
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.
140 *
141 * return Count of folder levels browsed up.
142 */
143 public int onBrowseUp() {
144 OCFile parentDir = null;
145 int moveCount = 0;
146
147 if(mFile != null){
148 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
149
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);
155 moveCount++;
156 } else {
157 parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH); // never returns null; keep the path in root folder
158 }
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);
163 moveCount++;
164 } // exit is granted because storageManager.getFileByPath("/") never returns null
165 mFile = parentDir;
166 }
167
168 if (mFile != null) {
169 listDirectory(mFile);
170
171 ((MoveActivity)mContainerActivity).startSyncFolderOperation(mFile);
172
173 // restore index and top position
174 restoreIndexAndTopPosition();
175
176 } // else - should never happen now
177
178 return moveCount;
179 }
180
181 /*
182 * Restore index and position
183 */
184 private void restoreIndexAndTopPosition() {
185 if (mIndexes.size() > 0) {
186 // needs to be checked; not every browse-up had a browse-down before
187
188 int index = mIndexes.remove(mIndexes.size() - 1);
189
190 int firstPosition = mFirstPositions.remove(mFirstPositions.size() -1);
191
192 int top = mTops.remove(mTops.size() - 1);
193
194 mList.setSelectionFromTop(firstPosition, top);
195
196 // Move the scroll if the selection is not visible
197 int indexPosition = mHeightCell*index;
198 int height = mList.getHeight();
199
200 if (indexPosition > height) {
201 if (android.os.Build.VERSION.SDK_INT >= 11)
202 {
203 mList.smoothScrollToPosition(index);
204 }
205 else if (android.os.Build.VERSION.SDK_INT >= 8)
206 {
207 mList.setSelectionFromTop(index, 0);
208 }
209
210 }
211 }
212 }
213
214 /*
215 * Save index and top position
216 */
217 private void saveIndexAndTopPosition(int index) {
218
219 mIndexes.add(index);
220
221 int firstPosition = mList.getFirstVisiblePosition();
222 mFirstPositions.add(firstPosition);
223
224 View view = mList.getChildAt(0);
225 int top = (view == null) ? 0 : view.getTop() ;
226
227 mTops.add(top);
228
229 // Save the height of a cell
230 mHeightCell = (view == null || mHeightCell != 0) ? mHeightCell : view.getHeight();
231 }
232
233 @Override
234 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
235 OCFile file = (OCFile) mAdapter.getItem(position);
236 if (file != null) {
237 if (file.isFolder()) {
238 // update state and view of this fragment
239 listDirectory(file);
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);
244
245 }
246
247 } else {
248 Log_OC.d(TAG, "Null object in ListAdapter!!");
249 }
250
251 }
252
253 /**
254 * Use this to query the {@link OCFile} that is currently
255 * being displayed by this fragment
256 * @return The currently viewed OCFile
257 */
258 public OCFile getCurrentFile(){
259 return mFile;
260 }
261
262 /**
263 * Calls {@link MoveFileListFragment#listDirectory(OCFile)} with a null parameter
264 */
265 public void listDirectory(){
266 listDirectory(null);
267 }
268
269 /**
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.
273 *
274 * @param directory File to be listed
275 */
276 public void listDirectory(OCFile directory) {
277 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
278 if (storageManager != null) {
279
280 // Check input parameters for null
281 if(directory == null){
282 if(mFile != null){
283 directory = mFile;
284 } else {
285 directory = storageManager.getFileByPath("/");
286 if (directory == null) return; // no files, wait for sync
287 }
288 }
289
290
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());
295 }
296
297 mAdapter.swapDirectory(directory, storageManager);
298 if (mFile == null || !mFile.equals(directory)) {
299 mList.setSelectionFromTop(0, 0);
300 }
301 mFile = directory;
302 }
303 }
304
305
306 @Override
307 public void onRefresh() {
308 super.onRefresh();
309
310 if (mFile != null) {
311 // Refresh mFile
312 mFile = mContainerActivity.getStorageManager().getFileById(mFile.getFileId());
313
314 listDirectory(mFile);
315
316 ((MoveActivity)mContainerActivity).startSyncFolderOperation(mFile);
317 }
318 }
319 }