2 * ownCloud Android client application
4 * @author Bartek Przybylski
6 * @author David A. Velasco
7 * Copyright (C) 2011 Bartek Przybylski
8 * Copyright (C) 2015 ownCloud Inc.
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 package com
.owncloud
.android
.ui
.fragment
;
25 import android
.app
.Activity
;
26 import android
.content
.Intent
;
27 import android
.os
.Bundle
;
28 import android
.support
.v4
.widget
.SwipeRefreshLayout
;
29 import android
.view
.ActionMode
;
30 import android
.view
.Menu
;
31 import android
.view
.MenuInflater
;
32 import android
.view
.MenuItem
;
33 import android
.view
.View
;
34 import android
.widget
.AbsListView
;
35 import android
.widget
.AdapterView
;
36 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
37 import android
.widget
.PopupMenu
;
39 import com
.owncloud
.android
.R
;
40 import com
.owncloud
.android
.authentication
.AccountUtils
;
41 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
42 import com
.owncloud
.android
.datamodel
.OCFile
;
43 import com
.owncloud
.android
.files
.FileMenuFilter
;
44 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
45 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
46 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
47 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
48 import com
.owncloud
.android
.ui
.activity
.FolderPickerActivity
;
49 import com
.owncloud
.android
.ui
.activity
.OnEnforceableRefreshListener
;
50 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
51 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
52 import com
.owncloud
.android
.ui
.dialog
.FileActionsDialogFragment
;
53 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
54 import com
.owncloud
.android
.ui
.dialog
.RemoveFilesDialogFragment
;
55 import com
.owncloud
.android
.ui
.dialog
.RenameFileDialogFragment
;
56 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
57 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
58 import com
.owncloud
.android
.utils
.FileStorageUtils
;
59 import com
.owncloud
.android
.ui
.preview
.PreviewTextFragment
;
62 import java
.util
.ArrayList
;
65 * A Fragment that lists all files and folders in a given path.
67 * TODO refactor to get rid of direct dependency on FileDisplayActivity
69 public class OCFileListFragment
extends ExtendedListFragment
{
71 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
73 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ?
74 OCFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
76 public final static String ARG_JUST_FOLDERS
= MY_PACKAGE
+ ".JUST_FOLDERS";
77 public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS
= MY_PACKAGE
+ ".ALLOW_CONTEXTUAL";
79 private static final String KEY_FILE
= MY_PACKAGE
+ ".extra.FILE";
81 private FileFragment
.ContainerActivity mContainerActivity
;
83 private OCFile mFile
= null
;
84 private FileListListAdapter mAdapter
;
85 private boolean mJustFolders
;
91 public void onAttach(Activity activity
) {
92 super.onAttach(activity
);
93 Log_OC
.e(TAG
, "onAttach");
95 mContainerActivity
= (FileFragment
.ContainerActivity
) activity
;
97 } catch (ClassCastException e
) {
98 throw new ClassCastException(activity
.toString() + " must implement " +
99 FileFragment
.ContainerActivity
.class.getSimpleName());
102 setOnRefreshListener((OnEnforceableRefreshListener
) activity
);
104 } catch (ClassCastException e
) {
105 throw new ClassCastException(activity
.toString() + " must implement " +
106 SwipeRefreshLayout
.OnRefreshListener
.class.getSimpleName());
112 public void onDetach() {
113 setOnRefreshListener(null
);
114 mContainerActivity
= null
;
122 public void onActivityCreated(Bundle savedInstanceState
) {
123 super.onActivityCreated(savedInstanceState
);
124 Log_OC
.e(TAG
, "onActivityCreated() start");
126 if (savedInstanceState
!= null
) {
127 mFile
= savedInstanceState
.getParcelable(KEY_FILE
);
131 setFooterEnabled(false
);
133 setFooterEnabled(true
);
136 Bundle args
= getArguments();
137 mJustFolders
= (args
== null
) ? false
: args
.getBoolean(ARG_JUST_FOLDERS
, false
);
138 mAdapter
= new FileListListAdapter(
143 setListAdapter(mAdapter
);
145 registerLongClickListener();
148 private void registerLongClickListener() {
149 getListView().setMultiChoiceModeListener(new AbsListView
.MultiChoiceModeListener() {
153 public void onItemCheckedStateChanged(ActionMode mode
, int position
, long id
, boolean checked
) {
154 final int checkedCount
= getListView().getCheckedItemCount();
155 // TODO Tobi extract to values
156 mode
.setTitle(checkedCount
+ " selected");
159 mAdapter
.setNewSelection(position
, checked
);
161 mAdapter
.removeSelection(position
);
164 // TODO maybe change: only recreate menu if count changes
166 if (checkedCount
== 1) {
167 createContextMenu(menu
);
169 // download, move, copy, delete
170 getActivity().getMenuInflater().inflate(R
.menu
.multiple_file_actions_menu
, menu
);
176 public boolean onCreateActionMode(ActionMode mode
, Menu menu
) {
182 public boolean onPrepareActionMode(ActionMode mode
, Menu menu
) {
187 public boolean onActionItemClicked(ActionMode mode
, MenuItem item
) {
188 return onFileActionChosen(item
.getItemId());
192 public void onDestroyActionMode(ActionMode mode
) {
193 mAdapter
.removeSelection();
199 private void showFileAction(int fileIndex
) {
200 Bundle args
= getArguments();
201 PopupMenu pm
= new PopupMenu(getActivity(),null
);
202 Menu menu
= pm
.getMenu();
204 boolean allowContextualActions
=
205 (args
== null
) ? true
: args
.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS
, true
);
207 if (allowContextualActions
) {
208 MenuInflater inflater
= getActivity().getMenuInflater();
210 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
211 OCFile targetFile
= (OCFile
) mAdapter
.getItem(fileIndex
);
213 if (mContainerActivity
.getStorageManager() != null
) {
214 FileMenuFilter mf
= new FileMenuFilter(
216 mContainerActivity
.getStorageManager().getAccount(),
223 /// TODO break this direct dependency on FileDisplayActivity... if possible
224 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
225 FileFragment frag
= ((FileDisplayActivity
)getActivity()).getSecondFragment();
226 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
227 frag
.getFile().getFileId() == targetFile
.getFileId()) {
228 item
= menu
.findItem(R
.id
.action_see_details
);
230 item
.setVisible(false
);
231 item
.setEnabled(false
);
235 FileActionsDialogFragment dialog
= FileActionsDialogFragment
.newInstance(menu
, fileIndex
, targetFile
.getFileName());
236 dialog
.setTargetFragment(this, 0);
237 dialog
.show(getFragmentManager(), FileActionsDialogFragment
.FTAG_FILE_ACTIONS
);
242 * Saves the current listed folder.
245 public void onSaveInstanceState(Bundle outState
) {
246 super.onSaveInstanceState(outState
);
247 outState
.putParcelable(KEY_FILE
, mFile
);
251 * Call this, when the user presses the up button.
253 * Tries to move up the current folder one level. If the parent folder was removed from the
254 * database, it continues browsing up until finding an existing folders.
256 * return Count of folder levels browsed up.
258 public int onBrowseUp() {
259 OCFile parentDir
= null
;
263 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
265 String parentPath
= null
;
266 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
267 parentPath
= new File(mFile
.getRemotePath()).getParent();
268 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
269 parentPath
+ OCFile
.PATH_SEPARATOR
;
270 parentDir
= storageManager
.getFileByPath(parentPath
);
273 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
);
275 while (parentDir
== null
) {
276 parentPath
= new File(parentPath
).getParent();
277 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
278 parentPath
+ OCFile
.PATH_SEPARATOR
;
279 parentDir
= storageManager
.getFileByPath(parentPath
);
281 } // exit is granted because storageManager.getFileByPath("/") never returns null
284 // TODO Enable when "On Device" is recovered ?
285 listDirectory(mFile
/*, MainApp.getOnlyOnDevice()*/);
289 // restore index and top position
290 restoreIndexAndTopPosition();
292 } // else - should never happen now
298 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
299 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
301 if (file
.isFolder()) {
302 // update state and view of this fragment
303 // TODO Enable when "On Device" is recovered ?
304 listDirectory(file
/*, MainApp.getOnlyOnDevice()*/);
305 // then, notify parent activity to let it update its state and view
306 mContainerActivity
.onBrowsedDownTo(file
);
307 // save index and top position
308 saveIndexAndTopPosition(position
);
310 } else { /// Click on a file
311 if (PreviewImageFragment
.canBePreviewed(file
)) {
312 // preview image - it handles the download, if needed
313 ((FileDisplayActivity
)mContainerActivity
).startImagePreview(file
);
314 } else if (PreviewTextFragment
.canBePreviewed(file
)){
315 ((FileDisplayActivity
)mContainerActivity
).startTextPreview(file
);
316 } else if (file
.isDown()) {
317 if (PreviewMediaFragment
.canBePreviewed(file
)) {
319 ((FileDisplayActivity
) mContainerActivity
).startMediaPreview(file
, 0, true
);
321 mContainerActivity
.getFileOperationsHelper().openFile(file
);
325 // automatic download, preview on finish
326 ((FileDisplayActivity
) mContainerActivity
).startDownloadForPreview(file
);
332 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
341 public void createContextMenu(Menu menu
) {
342 Bundle args
= getArguments();
343 boolean allowContextualActions
=
344 (args
== null
) ? true
: args
.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS
, true
);
345 if (allowContextualActions
) {
346 MenuInflater inflater
= getActivity().getMenuInflater();
347 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
348 OCFile targetFile
= null
;
349 if (mAdapter
.getCheckedItems().size() == 1){
350 targetFile
= mAdapter
.getCheckedItems().get(0);
353 if (mContainerActivity
.getStorageManager() != null
) {
354 FileMenuFilter mf
= new FileMenuFilter(
356 mContainerActivity
.getStorageManager().getAccount(),
363 /// TODO break this direct dependency on FileDisplayActivity... if possible
364 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
365 FileFragment frag
= ((FileDisplayActivity
)getActivity()).getSecondFragment();
366 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
367 frag
.getFile().getFileId() == targetFile
.getFileId()) {
368 item
= menu
.findItem(R
.id
.action_see_details
);
370 item
.setVisible(false
);
371 item
.setEnabled(false
);
377 public boolean onFileActionChosen(int menuId
) {
378 if (mAdapter
.getCheckedItems().size() == 1){
379 OCFile mTargetFile
= mAdapter
.getCheckedItems().get(0);
382 case R
.id
.action_share_file
: {
383 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
386 case R
.id
.action_open_file_with
: {
387 mContainerActivity
.getFileOperationsHelper().openFile(mTargetFile
);
390 case R
.id
.action_unshare_file
: {
391 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
394 case R
.id
.action_rename_file
: {
395 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(mTargetFile
);
396 dialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_RENAME_FILE
);
399 case R
.id
.action_remove_file
: {
400 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(mTargetFile
);
401 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
404 case R
.id
.action_download_file
:
405 case R
.id
.action_sync_file
: {
406 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
409 case R
.id
.action_cancel_download
:
410 case R
.id
.action_cancel_upload
: {
411 ((FileDisplayActivity
) mContainerActivity
).cancelTransference(mTargetFile
);
414 case R
.id
.action_see_details
: {
415 mContainerActivity
.showDetails(mTargetFile
);
418 case R
.id
.action_send_file
: {
420 if (!mTargetFile
.isDown()) { // Download the file
421 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
422 ((FileDisplayActivity
) mContainerActivity
).startDownloadForSending(mTargetFile
);
425 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(mTargetFile
);
429 case R
.id
.action_move
: {
430 Intent action
= new Intent(getActivity(), FolderPickerActivity
.class);
431 ArrayList files
= new ArrayList();
432 files
.add(mTargetFile
);
433 action
.putParcelableArrayListExtra(FolderPickerActivity
.EXTRA_FILES
, files
);
434 getActivity().startActivityForResult(action
, FileDisplayActivity
.ACTION_MOVE_FILES
);
437 case R
.id
.action_favorite_file
: {
438 mContainerActivity
.getFileOperationsHelper().toggleFavorite(mTargetFile
, true
);
441 case R
.id
.action_unfavorite_file
: {
442 mContainerActivity
.getFileOperationsHelper().toggleFavorite(mTargetFile
, false
);
445 case R
.id
.action_copy
:
446 Intent action
= new Intent(getActivity(), FolderPickerActivity
.class);
448 // Pass mTargetFile that contains info of selected file/folder
449 action
.putExtra(FolderPickerActivity
.EXTRA_FILE
, mTargetFile
);
450 getActivity().startActivityForResult(action
, FileDisplayActivity
.ACTION_COPY_FILES
);
456 ArrayList
<OCFile
> mTargetFiles
= mAdapter
.getCheckedItems();
459 case R
.id
.action_remove_file
: {
460 RemoveFilesDialogFragment dialog
= RemoveFilesDialogFragment
.newInstance(mTargetFiles
);
461 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
464 case R
.id
.action_download_file
:
465 case R
.id
.action_sync_file
: {
466 mContainerActivity
.getFileOperationsHelper().syncFiles(mTargetFiles
);
469 case R
.id
.action_move
: {
470 Intent action
= new Intent(getActivity(), FolderPickerActivity
.class);
471 action
.putParcelableArrayListExtra(FolderPickerActivity
.EXTRA_FILES
, mTargetFiles
);
472 getActivity().startActivityForResult(action
, FileDisplayActivity
.ACTION_MOVE_FILES
);
475 case R
.id
.action_favorite_file
: {
476 mContainerActivity
.getFileOperationsHelper().toggleFavorites(mTargetFiles
, true
);
479 case R
.id
.action_unfavorite_file
: {
480 mContainerActivity
.getFileOperationsHelper().toggleFavorites(mTargetFiles
, false
);
483 case R
.id
.action_copy
:
484 Intent action
= new Intent(getActivity(), FolderPickerActivity
.class);
485 action
.putParcelableArrayListExtra(FolderPickerActivity
.EXTRA_FILES
, mTargetFiles
);
486 getActivity().startActivityForResult(action
, FileDisplayActivity
.ACTION_COPY_FILES
);
499 public boolean onContextItemSelected (MenuItem item
) {
500 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
501 boolean matched
= onFileActionChosen(item
.getItemId());
503 return super.onContextItemSelected(item
);
511 * Use this to query the {@link OCFile} that is currently
512 * being displayed by this fragment
514 * @return The currently viewed OCFile
516 public OCFile
getCurrentFile() {
521 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
523 public void listDirectory(/*boolean onlyOnDevice*/){
525 // TODO Enable when "On Device" is recovered ?
526 // listDirectory(null, onlyOnDevice);
529 public void refreshDirectory(){
530 // TODO Enable when "On Device" is recovered ?
531 listDirectory(getCurrentFile()/*, MainApp.getOnlyOnDevice()*/);
535 * Lists the given directory on the view. When the input parameter is null,
536 * it will either refresh the last known directory. list the root
537 * if there never was a directory.
539 * @param directory File to be listed
541 public void listDirectory(OCFile directory
/*, boolean onlyOnDevice*/) {
542 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
543 if (storageManager
!= null
) {
545 // Check input parameters for null
546 if (directory
== null
) {
550 directory
= storageManager
.getFileByPath("/");
551 if (directory
== null
) return; // no files, wait for sync
556 // If that's not a directory -> List its parent
557 if (!directory
.isFolder()) {
558 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
559 directory
= storageManager
.getFileById(directory
.getParentId());
562 // TODO Enable when "On Device" is recovered ?
563 mAdapter
.swapDirectory(directory
, storageManager
/*, onlyOnDevice*/);
564 if (mFile
== null
|| !mFile
.equals(directory
)) {
565 mCurrentListView
.setSelection(0);
574 private void updateLayout() {
576 int filesCount
= 0, foldersCount
= 0, imagesCount
= 0;
577 int count
= mAdapter
.getCount();
579 for (int i
=0; i
< count
; i
++) {
580 file
= (OCFile
) mAdapter
.getItem(i
);
581 if (file
.isFolder()) {
584 if (!file
.isHidden()) {
587 if (file
.isImage()) {
594 setFooterText(generateFooterText(filesCount
, foldersCount
));
596 // decide grid vs list view
597 OwnCloudVersion version
= AccountUtils
.getServerVersion(
598 ((FileActivity
)mContainerActivity
).getAccount());
599 if (version
!= null
&& version
.supportsRemoteThumbnails() &&
600 imagesCount
> 0 && imagesCount
== filesCount
) {
602 registerLongClickListener();
609 private String
generateFooterText(int filesCount
, int foldersCount
) {
611 if (filesCount
<= 0) {
612 if (foldersCount
<= 0) {
615 } else if (foldersCount
== 1) {
616 output
= getResources().getString(R
.string
.file_list__footer__folder
);
618 } else { // foldersCount > 1
619 output
= getResources().getString(R
.string
.file_list__footer__folders
, foldersCount
);
622 } else if (filesCount
== 1) {
623 if (foldersCount
<= 0) {
624 output
= getResources().getString(R
.string
.file_list__footer__file
);
626 } else if (foldersCount
== 1) {
627 output
= getResources().getString(R
.string
.file_list__footer__file_and_folder
);
629 } else { // foldersCount > 1
630 output
= getResources().getString(R
.string
.file_list__footer__file_and_folders
, foldersCount
);
632 } else { // filesCount > 1
633 if (foldersCount
<= 0) {
634 output
= getResources().getString(R
.string
.file_list__footer__files
, filesCount
);
636 } else if (foldersCount
== 1) {
637 output
= getResources().getString(R
.string
.file_list__footer__files_and_folder
, filesCount
);
639 } else { // foldersCount > 1
640 output
= getResources().getString(
641 R
.string
.file_list__footer__files_and_folders
, filesCount
, foldersCount
649 public void sortByName(boolean descending
) {
650 mAdapter
.setSortOrder(FileStorageUtils
.SORT_NAME
, descending
);
653 public void sortByDate(boolean descending
) {
654 mAdapter
.setSortOrder(FileStorageUtils
.SORT_DATE
, descending
);
657 public void sortBySize(boolean descending
) {
658 mAdapter
.setSortOrder(FileStorageUtils
.SORT_SIZE
, descending
);