renamed sortDirectory to sortFolder
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / OCFileListFragment.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
22 import android.app.Activity;
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.support.v4.widget.SwipeRefreshLayout;
26 import android.view.ContextMenu;
27 import android.view.MenuInflater;
28 import android.view.MenuItem;
29 import android.view.View;
30 import android.widget.AdapterView;
31 import android.widget.AdapterView.AdapterContextMenuInfo;
32
33 import com.owncloud.android.R;
34 import com.owncloud.android.datamodel.FileDataStorageManager;
35 import com.owncloud.android.datamodel.OCFile;
36 import com.owncloud.android.files.FileMenuFilter;
37 import com.owncloud.android.lib.common.utils.Log_OC;
38 import com.owncloud.android.ui.activity.FileDisplayActivity;
39 import com.owncloud.android.ui.activity.MoveActivity;
40 import com.owncloud.android.ui.activity.OnEnforceableRefreshListener;
41 import com.owncloud.android.ui.adapter.FileListListAdapter;
42 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
43 import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
44 import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
45 import com.owncloud.android.ui.preview.PreviewImageFragment;
46 import com.owncloud.android.ui.preview.PreviewMediaFragment;
47 import com.owncloud.android.utils.FileStorageUtils;
48
49 /**
50 * A Fragment that lists all files and folders in a given path.
51 *
52 * TODO refactorize to get rid of direct dependency on FileDisplayActivity
53 *
54 * @author Bartek Przybylski
55 * @author masensio
56 * @author David A. Velasco
57 */
58 public class OCFileListFragment extends ExtendedListFragment {
59
60 private static final String TAG = OCFileListFragment.class.getSimpleName();
61
62 private static final String MY_PACKAGE = OCFileListFragment.class.getPackage() != null ?
63 OCFileListFragment.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
64
65 public final static String ARG_JUST_FOLDERS = MY_PACKAGE + ".JUST_FOLDERS";
66 public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS = MY_PACKAGE + ".ALLOW_CONTEXTUAL";
67
68 private static final String KEY_FILE = MY_PACKAGE + ".extra.FILE";
69
70 private FileFragment.ContainerActivity mContainerActivity;
71
72 private OCFile mFile = null;
73 private FileListListAdapter mAdapter;
74
75 private OCFile mTargetFile;
76
77
78 /**
79 * {@inheritDoc}
80 */
81 @Override
82 public void onAttach(Activity activity) {
83 super.onAttach(activity);
84 Log_OC.e(TAG, "onAttach");
85 try {
86 mContainerActivity = (FileFragment.ContainerActivity) activity;
87
88 } catch (ClassCastException e) {
89 throw new ClassCastException(activity.toString() + " must implement " +
90 FileFragment.ContainerActivity.class.getSimpleName());
91 }
92 try {
93 setOnRefreshListener((OnEnforceableRefreshListener) activity);
94
95 } catch (ClassCastException e) {
96 throw new ClassCastException(activity.toString() + " must implement " +
97 SwipeRefreshLayout.OnRefreshListener.class.getSimpleName());
98 }
99 }
100
101
102 @Override
103 public void onDetach() {
104 setOnRefreshListener(null);
105 mContainerActivity = null;
106 super.onDetach();
107 }
108
109 /**
110 * {@inheritDoc}
111 */
112 @Override
113 public void onActivityCreated(Bundle savedInstanceState) {
114 super.onActivityCreated(savedInstanceState);
115 Log_OC.e(TAG, "onActivityCreated() start");
116
117 if (savedInstanceState != null) {
118 mFile = savedInstanceState.getParcelable(KEY_FILE);
119 }
120
121 Bundle args = getArguments();
122 boolean justFolders = (args == null) ? false : args.getBoolean(ARG_JUST_FOLDERS, false);
123 mAdapter = new FileListListAdapter(
124 justFolders,
125 getSherlockActivity(),
126 mContainerActivity
127 );
128 setListAdapter(mAdapter);
129
130 registerForContextMenu(getListView());
131 getListView().setOnCreateContextMenuListener(this);
132 }
133
134 /**
135 * Saves the current listed folder.
136 */
137 @Override
138 public void onSaveInstanceState (Bundle outState) {
139 super.onSaveInstanceState(outState);
140 outState.putParcelable(KEY_FILE, mFile);
141 }
142
143 /**
144 * Call this, when the user presses the up button.
145 *
146 * Tries to move up the current folder one level. If the parent folder was removed from the
147 * database, it continues browsing up until finding an existing folders.
148 *
149 * return Count of folder levels browsed up.
150 */
151 public int onBrowseUp() {
152 OCFile parentDir = null;
153 int moveCount = 0;
154
155 if(mFile != null){
156 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
157
158 String parentPath = null;
159 if (mFile.getParentId() != FileDataStorageManager.ROOT_PARENT_ID) {
160 parentPath = new File(mFile.getRemotePath()).getParent();
161 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
162 parentPath + OCFile.PATH_SEPARATOR;
163 parentDir = storageManager.getFileByPath(parentPath);
164 moveCount++;
165 } else {
166 parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH);
167 }
168 while (parentDir == null) {
169 parentPath = new File(parentPath).getParent();
170 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
171 parentPath + OCFile.PATH_SEPARATOR;
172 parentDir = storageManager.getFileByPath(parentPath);
173 moveCount++;
174 } // exit is granted because storageManager.getFileByPath("/") never returns null
175 mFile = parentDir;
176
177 listDirectory(mFile);
178
179 onRefresh(false);
180
181 // restore index and top position
182 restoreIndexAndTopPosition();
183
184 } // else - should never happen now
185
186 return moveCount;
187 }
188
189 @Override
190 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
191 OCFile file = (OCFile) mAdapter.getItem(position);
192 if (file != null) {
193 if (file.isFolder()) {
194 // update state and view of this fragment
195 listDirectory(file);
196 // then, notify parent activity to let it update its state and view
197 mContainerActivity.onBrowsedDownTo(file);
198 // save index and top position
199 saveIndexAndTopPosition(position);
200
201 } else { /// Click on a file
202 if (PreviewImageFragment.canBePreviewed(file)) {
203 // preview image - it handles the download, if needed
204 ((FileDisplayActivity)mContainerActivity).startImagePreview(file);
205
206 } else if (file.isDown()) {
207 if (PreviewMediaFragment.canBePreviewed(file)) {
208 // media preview
209 ((FileDisplayActivity)mContainerActivity).startMediaPreview(file, 0, true);
210 } else {
211 mContainerActivity.getFileOperationsHelper().openFile(file);
212 }
213
214 } else {
215 // automatic download, preview on finish
216 ((FileDisplayActivity)mContainerActivity).startDownloadForPreview(file);
217 }
218
219 }
220
221 } else {
222 Log_OC.d(TAG, "Null object in ListAdapter!!");
223 }
224
225 }
226
227 /**
228 * {@inheritDoc}
229 */
230 @Override
231 public void onCreateContextMenu (
232 ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
233 super.onCreateContextMenu(menu, v, menuInfo);
234 Bundle args = getArguments();
235 boolean allowContextualActions =
236 (args == null) ? true : args.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS, true);
237 if (allowContextualActions) {
238 MenuInflater inflater = getSherlockActivity().getMenuInflater();
239 inflater.inflate(R.menu.file_actions_menu, menu);
240 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
241 OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
242
243 if (mContainerActivity.getStorageManager() != null) {
244 FileMenuFilter mf = new FileMenuFilter(
245 targetFile,
246 mContainerActivity.getStorageManager().getAccount(),
247 mContainerActivity,
248 getSherlockActivity()
249 );
250 mf.filter(menu);
251 }
252
253 /// additional restrictions for this fragment
254 // TODO allow in the future 'open with' for previewable files
255 MenuItem item = menu.findItem(R.id.action_open_file_with);
256 if (item != null) {
257 item.setVisible(false);
258 item.setEnabled(false);
259 }
260 /// TODO break this direct dependency on FileDisplayActivity... if possible
261 FileFragment frag = ((FileDisplayActivity)getSherlockActivity()).getSecondFragment();
262 if (frag != null && frag instanceof FileDetailFragment &&
263 frag.getFile().getFileId() == targetFile.getFileId()) {
264 item = menu.findItem(R.id.action_see_details);
265 if (item != null) {
266 item.setVisible(false);
267 item.setEnabled(false);
268 }
269 }
270 }
271 }
272
273
274 /**
275 * {@inhericDoc}
276 */
277 @Override
278 public boolean onContextItemSelected (MenuItem item) {
279 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
280 mTargetFile = (OCFile) mAdapter.getItem(info.position);
281 switch (item.getItemId()) {
282 case R.id.action_share_file: {
283 mContainerActivity.getFileOperationsHelper().shareFileWithLink(mTargetFile);
284 return true;
285 }
286 case R.id.action_unshare_file: {
287 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(mTargetFile);
288 return true;
289 }
290 case R.id.action_rename_file: {
291 RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(mTargetFile);
292 dialog.show(getFragmentManager(), FileDetailFragment.FTAG_RENAME_FILE);
293 return true;
294 }
295 case R.id.action_remove_file: {
296 RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(mTargetFile);
297 dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
298 return true;
299 }
300 case R.id.action_download_file:
301 case R.id.action_sync_file: {
302 mContainerActivity.getFileOperationsHelper().syncFile(mTargetFile);
303 return true;
304 }
305 case R.id.action_cancel_download:
306 case R.id.action_cancel_upload: {
307 ((FileDisplayActivity)mContainerActivity).cancelTransference(mTargetFile);
308 return true;
309 }
310 case R.id.action_see_details: {
311 mContainerActivity.showDetails(mTargetFile);
312 return true;
313 }
314 case R.id.action_send_file: {
315 // Obtain the file
316 if (!mTargetFile.isDown()) { // Download the file
317 Log_OC.d(TAG, mTargetFile.getRemotePath() + " : File must be downloaded");
318 ((FileDisplayActivity)mContainerActivity).startDownloadForSending(mTargetFile);
319
320 } else {
321 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(mTargetFile);
322 }
323 return true;
324 }
325 case R.id.action_move: {
326 Intent action = new Intent(getActivity(), MoveActivity.class);
327
328 // Pass mTargetFile that contains info of selected file/folder
329 action.putExtra(MoveActivity.EXTRA_TARGET_FILE, mTargetFile);
330 getActivity().startActivityForResult(action, FileDisplayActivity.ACTION_MOVE_FILES);
331 return true;
332 }
333 default:
334 return super.onContextItemSelected(item);
335 }
336 }
337
338
339 /**
340 * Use this to query the {@link OCFile} that is currently
341 * being displayed by this fragment
342 * @return The currently viewed OCFile
343 */
344 public OCFile getCurrentFile(){
345 return mFile;
346 }
347
348 /**
349 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
350 */
351 public void listDirectory(){
352 listDirectory(null);
353 }
354
355 /**
356 * Lists the given directory on the view. When the input parameter is null,
357 * it will either refresh the last known directory. list the root
358 * if there never was a directory.
359 *
360 * @param directory File to be listed
361 */
362 public void listDirectory(OCFile directory) {
363 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
364 if (storageManager != null) {
365
366 // Check input parameters for null
367 if(directory == null){
368 if(mFile != null){
369 directory = mFile;
370 } else {
371 directory = storageManager.getFileByPath("/");
372 if (directory == null) return; // no files, wait for sync
373 }
374 }
375
376
377 // If that's not a directory -> List its parent
378 if(!directory.isFolder()){
379 Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
380 directory = storageManager.getFileById(directory.getParentId());
381 }
382
383 mAdapter.swapDirectory(directory, storageManager);
384 if (mFile == null || !mFile.equals(directory)) {
385 mList.setSelectionFromTop(0, 0);
386 }
387 mFile = directory;
388 }
389 }
390
391 public void sortByName(boolean descending) {
392 mAdapter.setSortOrder(FileStorageUtils.SORT_NAME, descending);
393 }
394
395 public void sortByDate(boolean descending) {
396 mAdapter.setSortOrder(FileStorageUtils.SORT_DATE, descending);
397 }
398
399 public void sortBySize(boolean descending) {
400 mAdapter.setSortOrder(FileStorageUtils.SORT_SIZE, descending);
401 }
402
403 }