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
.Build
;
28 import android
.os
.Bundle
;
29 import android
.support
.v4
.widget
.SwipeRefreshLayout
;
30 import android
.view
.ContextMenu
;
31 import android
.view
.Menu
;
32 import android
.view
.MenuInflater
;
33 import android
.view
.MenuItem
;
34 import android
.view
.View
;
35 import android
.widget
.AdapterView
;
36 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
37 import android
.widget
.PopupMenu
;
38 import android
.widget
.Toast
;
40 import com
.owncloud
.android
.R
;
41 import com
.owncloud
.android
.authentication
.AccountUtils
;
42 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
43 import com
.owncloud
.android
.datamodel
.OCFile
;
44 import com
.owncloud
.android
.files
.FileMenuFilter
;
45 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
46 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
47 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
48 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
49 import com
.owncloud
.android
.ui
.activity
.FolderPickerActivity
;
50 import com
.owncloud
.android
.ui
.activity
.OnEnforceableRefreshListener
;
51 import com
.owncloud
.android
.ui
.activity
.UploadFilesActivity
;
52 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
53 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
54 import com
.owncloud
.android
.ui
.dialog
.CreateFolderDialogFragment
;
55 import com
.owncloud
.android
.ui
.dialog
.FileActionsDialogFragment
;
56 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
57 import com
.owncloud
.android
.ui
.dialog
.RenameFileDialogFragment
;
58 import com
.owncloud
.android
.ui
.dialog
.UploadSourceDialogFragment
;
59 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
60 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
61 import com
.owncloud
.android
.utils
.FileStorageUtils
;
62 import com
.owncloud
.android
.ui
.preview
.PreviewTextFragment
;
67 * A Fragment that lists all files and folders in a given path.
69 * TODO refactor to get rid of direct dependency on FileDisplayActivity
71 public class OCFileListFragment
extends ExtendedListFragment
implements FileActionsDialogFragment
.FileActionsDialogFragmentListener
{
73 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
75 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ?
76 OCFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
78 public final static String ARG_JUST_FOLDERS
= MY_PACKAGE
+ ".JUST_FOLDERS";
79 public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS
= MY_PACKAGE
+ ".ALLOW_CONTEXTUAL";
81 private static final String KEY_FILE
= MY_PACKAGE
+ ".extra.FILE";
83 private static String DIALOG_CREATE_FOLDER
= "DIALOG_CREATE_FOLDER";
85 private FileFragment
.ContainerActivity mContainerActivity
;
87 private OCFile mFile
= null
;
88 private FileListListAdapter mAdapter
;
89 private boolean mJustFolders
;
91 private OCFile mTargetFile
;
99 public void onAttach(Activity activity
) {
100 super.onAttach(activity
);
101 Log_OC
.e(TAG
, "onAttach");
103 mContainerActivity
= (FileFragment
.ContainerActivity
) activity
;
105 } catch (ClassCastException e
) {
106 throw new ClassCastException(activity
.toString() + " must implement " +
107 FileFragment
.ContainerActivity
.class.getSimpleName());
110 setOnRefreshListener((OnEnforceableRefreshListener
) activity
);
112 } catch (ClassCastException e
) {
113 throw new ClassCastException(activity
.toString() + " must implement " +
114 SwipeRefreshLayout
.OnRefreshListener
.class.getSimpleName());
120 public void onDetach() {
121 setOnRefreshListener(null
);
122 mContainerActivity
= null
;
130 public void onActivityCreated(Bundle savedInstanceState
) {
131 super.onActivityCreated(savedInstanceState
);
132 Log_OC
.e(TAG
, "onActivityCreated() start");
134 if (savedInstanceState
!= null
) {
135 mFile
= savedInstanceState
.getParcelable(KEY_FILE
);
139 setFooterEnabled(false
);
141 setFooterEnabled(true
);
144 Bundle args
= getArguments();
145 mJustFolders
= (args
== null
) ? false
: args
.getBoolean(ARG_JUST_FOLDERS
, false
);
146 mAdapter
= new FileListListAdapter(
151 setListAdapter(mAdapter
);
153 registerLongClickListener();
154 registerFabListeners();
157 private void registerFabListeners() {
158 registerFabUploadListeners();
159 registerFabMkDirListeners();
160 registerFabUploadFromAppListeners();
163 private void registerFabUploadListeners() {
164 getFabUpload().setOnClickListener(new View
.OnClickListener() {
166 public void onClick(View v
) {
167 Intent action
= new Intent(getActivity(), UploadFilesActivity
.class);
169 UploadFilesActivity
.EXTRA_ACCOUNT
,
170 ((FileActivity
) getActivity()).getAccount()
172 getActivity().startActivityForResult(action
, UploadSourceDialogFragment
.ACTION_SELECT_MULTIPLE_FILES
);
173 getFabMain().collapse();
177 getFabUpload().setOnLongClickListener(new View
.OnLongClickListener() {
179 public boolean onLongClick(View v
) {
180 Toast
.makeText(getActivity(), R
.string
.actionbar_upload
, Toast
.LENGTH_SHORT
).show();
186 private void registerFabMkDirListeners() {
187 getFabMkdir().setOnClickListener(new View
.OnClickListener() {
189 public void onClick(View v
) {
190 CreateFolderDialogFragment dialog
=
191 CreateFolderDialogFragment
.newInstance(mFile
);
192 dialog
.show(getActivity().getSupportFragmentManager(), FileDisplayActivity
.DIALOG_CREATE_FOLDER
);
193 getFabMain().collapse();
197 getFabMkdir().setOnLongClickListener(new View
.OnLongClickListener() {
199 public boolean onLongClick(View v
) {
200 Toast
.makeText(getActivity(), R
.string
.actionbar_mkdir
, Toast
.LENGTH_SHORT
).show();
206 private void registerFabUploadFromAppListeners() {
207 getFabUploadFromApp().setOnClickListener(new View
.OnClickListener() {
209 public void onClick(View v
) {
210 Intent action
= new Intent(Intent
.ACTION_GET_CONTENT
);
211 action
= action
.setType("*/*").addCategory(Intent
.CATEGORY_OPENABLE
);
213 //Intent.EXTRA_ALLOW_MULTIPLE is only supported on api level 18+, Jelly Bean
214 if (Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.JELLY_BEAN_MR2
) {
215 action
.putExtra(Intent
.EXTRA_ALLOW_MULTIPLE
, true
);
218 getActivity().startActivityForResult(
219 Intent
.createChooser(action
, getString(R
.string
.upload_chooser_title
)),
220 UploadSourceDialogFragment
.ACTION_SELECT_CONTENT_FROM_APPS
222 getFabMain().collapse();
226 getFabUploadFromApp().setOnLongClickListener(new View
.OnLongClickListener() {
228 public boolean onLongClick(View v
) {
229 Toast
.makeText(getActivity(),
230 R
.string
.actionbar_upload_from_apps
,
231 Toast
.LENGTH_SHORT
).show();
237 private void registerLongClickListener() {
238 getListView().setOnItemLongClickListener(new AdapterView
.OnItemLongClickListener() {
239 public boolean onItemLongClick(AdapterView
<?
> arg0
, View v
,
240 int index
, long arg3
) {
241 showFileAction(index
);
248 private void showFileAction(int fileIndex
) {
249 Bundle args
= getArguments();
250 PopupMenu pm
= new PopupMenu(getActivity(),null
);
251 Menu menu
= pm
.getMenu();
253 boolean allowContextualActions
=
254 (args
== null
) ? true
: args
.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS
, true
);
256 if (allowContextualActions
) {
257 MenuInflater inflater
= getActivity().getMenuInflater();
259 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
260 OCFile targetFile
= (OCFile
) mAdapter
.getItem(fileIndex
);
262 if (mContainerActivity
.getStorageManager() != null
) {
263 FileMenuFilter mf
= new FileMenuFilter(
265 mContainerActivity
.getStorageManager().getAccount(),
272 /// TODO break this direct dependency on FileDisplayActivity... if possible
273 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
274 FileFragment frag
= ((FileDisplayActivity
)getActivity()).getSecondFragment();
275 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
276 frag
.getFile().getFileId() == targetFile
.getFileId()) {
277 item
= menu
.findItem(R
.id
.action_see_details
);
279 item
.setVisible(false
);
280 item
.setEnabled(false
);
284 FileActionsDialogFragment dialog
= FileActionsDialogFragment
.newInstance(menu
, fileIndex
, targetFile
.getFileName());
285 dialog
.setTargetFragment(this, 0);
286 dialog
.show(getFragmentManager(), FileActionsDialogFragment
.FTAG_FILE_ACTIONS
);
291 * Saves the current listed folder.
294 public void onSaveInstanceState(Bundle outState
) {
295 super.onSaveInstanceState(outState
);
296 outState
.putParcelable(KEY_FILE
, mFile
);
300 * Call this, when the user presses the up button.
302 * Tries to move up the current folder one level. If the parent folder was removed from the
303 * database, it continues browsing up until finding an existing folders.
305 * return Count of folder levels browsed up.
307 public int onBrowseUp() {
308 OCFile parentDir
= null
;
312 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
314 String parentPath
= null
;
315 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
316 parentPath
= new File(mFile
.getRemotePath()).getParent();
317 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
318 parentPath
+ OCFile
.PATH_SEPARATOR
;
319 parentDir
= storageManager
.getFileByPath(parentPath
);
322 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
);
324 while (parentDir
== null
) {
325 parentPath
= new File(parentPath
).getParent();
326 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
327 parentPath
+ OCFile
.PATH_SEPARATOR
;
328 parentDir
= storageManager
.getFileByPath(parentPath
);
330 } // exit is granted because storageManager.getFileByPath("/") never returns null
333 // TODO Enable when "On Device" is recovered ?
334 listDirectory(mFile
/*, MainApp.getOnlyOnDevice()*/);
338 // restore index and top position
339 restoreIndexAndTopPosition();
341 } // else - should never happen now
347 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
348 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
350 if (file
.isFolder()) {
351 // update state and view of this fragment
352 // TODO Enable when "On Device" is recovered ?
353 listDirectory(file
/*, MainApp.getOnlyOnDevice()*/);
354 // then, notify parent activity to let it update its state and view
355 mContainerActivity
.onBrowsedDownTo(file
);
356 // save index and top position
357 saveIndexAndTopPosition(position
);
359 } else { /// Click on a file
360 if (PreviewImageFragment
.canBePreviewed(file
)) {
361 // preview image - it handles the download, if needed
362 ((FileDisplayActivity
)mContainerActivity
).startImagePreview(file
);
363 } else if (PreviewTextFragment
.canBePreviewed(file
)){
364 ((FileDisplayActivity
)mContainerActivity
).startTextPreview(file
);
365 } else if (file
.isDown()) {
366 if (PreviewMediaFragment
.canBePreviewed(file
)) {
368 ((FileDisplayActivity
) mContainerActivity
).startMediaPreview(file
, 0, true
);
370 mContainerActivity
.getFileOperationsHelper().openFile(file
);
374 // automatic download, preview on finish
375 ((FileDisplayActivity
) mContainerActivity
).startDownloadForPreview(file
);
381 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
390 public void onCreateContextMenu(
391 ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
392 Bundle args
= getArguments();
393 boolean allowContextualActions
=
394 (args
== null
) ? true
: args
.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS
, true
);
395 if (allowContextualActions
) {
396 MenuInflater inflater
= getActivity().getMenuInflater();
397 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
398 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
399 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
401 if (mContainerActivity
.getStorageManager() != null
) {
402 FileMenuFilter mf
= new FileMenuFilter(
404 mContainerActivity
.getStorageManager().getAccount(),
411 /// TODO break this direct dependency on FileDisplayActivity... if possible
412 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
413 FileFragment frag
= ((FileDisplayActivity
)getActivity()).getSecondFragment();
414 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
415 frag
.getFile().getFileId() == targetFile
.getFileId()) {
416 item
= menu
.findItem(R
.id
.action_see_details
);
418 item
.setVisible(false
);
419 item
.setEnabled(false
);
429 public boolean onFileActionChosen(int menuId
, int filePosition
) {
430 mTargetFile
= (OCFile
) mAdapter
.getItem(filePosition
);
432 case R
.id
.action_share_file
: {
433 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
436 case R
.id
.action_open_file_with
: {
437 mContainerActivity
.getFileOperationsHelper().openFile(mTargetFile
);
440 case R
.id
.action_unshare_file
: {
441 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
444 case R
.id
.action_rename_file
: {
445 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(mTargetFile
);
446 dialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_RENAME_FILE
);
449 case R
.id
.action_remove_file
: {
450 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(mTargetFile
);
451 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
454 case R
.id
.action_download_file
:
455 case R
.id
.action_sync_file
: {
456 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
459 case R
.id
.action_cancel_download
:
460 case R
.id
.action_cancel_upload
: {
461 ((FileDisplayActivity
) mContainerActivity
).cancelTransference(mTargetFile
);
464 case R
.id
.action_see_details
: {
465 mContainerActivity
.showDetails(mTargetFile
);
468 case R
.id
.action_send_file
: {
470 if (!mTargetFile
.isDown()) { // Download the file
471 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
472 ((FileDisplayActivity
) mContainerActivity
).startDownloadForSending(mTargetFile
);
475 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(mTargetFile
);
479 case R
.id
.action_move
: {
480 Intent action
= new Intent(getActivity(), FolderPickerActivity
.class);
482 // Pass mTargetFile that contains info of selected file/folder
483 action
.putExtra(FolderPickerActivity
.EXTRA_FILE
, mTargetFile
);
484 getActivity().startActivityForResult(action
, FileDisplayActivity
.ACTION_MOVE_FILES
);
487 case R
.id
.action_favorite_file
: {
488 mContainerActivity
.getFileOperationsHelper().toggleFavorite(mTargetFile
, true
);
491 case R
.id
.action_unfavorite_file
: {
492 mContainerActivity
.getFileOperationsHelper().toggleFavorite(mTargetFile
, false
);
495 case R
.id
.action_copy
:
496 Intent action
= new Intent(getActivity(), FolderPickerActivity
.class);
498 // Pass mTargetFile that contains info of selected file/folder
499 action
.putExtra(FolderPickerActivity
.EXTRA_FILE
, mTargetFile
);
500 getActivity().startActivityForResult(action
, FileDisplayActivity
.ACTION_COPY_FILES
);
511 public boolean onContextItemSelected (MenuItem item
) {
512 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
513 boolean matched
= onFileActionChosen(item
.getItemId(), ((AdapterContextMenuInfo
) item
.getMenuInfo()).position
);
515 return super.onContextItemSelected(item
);
523 * Use this to query the {@link OCFile} that is currently
524 * being displayed by this fragment
526 * @return The currently viewed OCFile
528 public OCFile
getCurrentFile() {
533 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
535 public void listDirectory(/*boolean onlyOnDevice*/){
537 // TODO Enable when "On Device" is recovered ?
538 // listDirectory(null, onlyOnDevice);
541 public void refreshDirectory(){
542 // TODO Enable when "On Device" is recovered ?
543 listDirectory(getCurrentFile()/*, MainApp.getOnlyOnDevice()*/);
547 * Lists the given directory on the view. When the input parameter is null,
548 * it will either refresh the last known directory. list the root
549 * if there never was a directory.
551 * @param directory File to be listed
553 public void listDirectory(OCFile directory
/*, boolean onlyOnDevice*/) {
554 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
555 if (storageManager
!= null
) {
557 // Check input parameters for null
558 if (directory
== null
) {
562 directory
= storageManager
.getFileByPath("/");
563 if (directory
== null
) return; // no files, wait for sync
568 // If that's not a directory -> List its parent
569 if (!directory
.isFolder()) {
570 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
571 directory
= storageManager
.getFileById(directory
.getParentId());
574 // TODO Enable when "On Device" is recovered ?
575 mAdapter
.swapDirectory(directory
, storageManager
/*, onlyOnDevice*/);
576 if (mFile
== null
|| !mFile
.equals(directory
)) {
577 mCurrentListView
.setSelection(0);
586 private void updateLayout() {
588 int filesCount
= 0, foldersCount
= 0, imagesCount
= 0;
589 int count
= mAdapter
.getCount();
591 for (int i
=0; i
< count
; i
++) {
592 file
= (OCFile
) mAdapter
.getItem(i
);
593 if (file
.isFolder()) {
596 if (!file
.isHidden()) {
599 if (file
.isImage()) {
606 setFooterText(generateFooterText(filesCount
, foldersCount
));
608 // decide grid vs list view
609 OwnCloudVersion version
= AccountUtils
.getServerVersion(
610 ((FileActivity
)mContainerActivity
).getAccount());
611 if (version
!= null
&& version
.supportsRemoteThumbnails() &&
612 imagesCount
> 0 && imagesCount
== filesCount
) {
614 registerLongClickListener();
621 private String
generateFooterText(int filesCount
, int foldersCount
) {
623 if (filesCount
<= 0) {
624 if (foldersCount
<= 0) {
627 } else if (foldersCount
== 1) {
628 output
= getResources().getString(R
.string
.file_list__footer__folder
);
630 } else { // foldersCount > 1
631 output
= getResources().getString(R
.string
.file_list__footer__folders
, foldersCount
);
634 } else if (filesCount
== 1) {
635 if (foldersCount
<= 0) {
636 output
= getResources().getString(R
.string
.file_list__footer__file
);
638 } else if (foldersCount
== 1) {
639 output
= getResources().getString(R
.string
.file_list__footer__file_and_folder
);
641 } else { // foldersCount > 1
642 output
= getResources().getString(R
.string
.file_list__footer__file_and_folders
, foldersCount
);
644 } else { // filesCount > 1
645 if (foldersCount
<= 0) {
646 output
= getResources().getString(R
.string
.file_list__footer__files
, filesCount
);
648 } else if (foldersCount
== 1) {
649 output
= getResources().getString(R
.string
.file_list__footer__files_and_folder
, filesCount
);
651 } else { // foldersCount > 1
652 output
= getResources().getString(
653 R
.string
.file_list__footer__files_and_folders
, filesCount
, foldersCount
661 public void sortByName(boolean descending
) {
662 mAdapter
.setSortOrder(FileStorageUtils
.SORT_NAME
, descending
);
665 public void sortByDate(boolean descending
) {
666 mAdapter
.setSortOrder(FileStorageUtils
.SORT_DATE
, descending
);
669 public void sortBySize(boolean descending
) {
670 mAdapter
.setSortOrder(FileStorageUtils
.SORT_SIZE
, descending
);