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
;
27 import android
.app
.Activity
;
28 import android
.content
.Intent
;
29 import android
.os
.Bundle
;
30 import android
.support
.v4
.widget
.SwipeRefreshLayout
;
31 import android
.view
.ContextMenu
;
32 import android
.view
.LayoutInflater
;
33 import android
.view
.Menu
;
34 import android
.view
.MenuInflater
;
35 import android
.view
.MenuItem
;
36 import android
.view
.View
;
37 import android
.view
.ViewGroup
;
38 import android
.widget
.AdapterView
;
39 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
40 import android
.widget
.ListView
;
41 import android
.widget
.PopupMenu
;
42 import android
.widget
.Toast
;
44 import com
.getbase
.floatingactionbutton
.FloatingActionsMenu
;
45 import com
.owncloud
.android
.R
;
46 import com
.owncloud
.android
.authentication
.AccountUtils
;
47 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
48 import com
.owncloud
.android
.datamodel
.OCFile
;
49 import com
.owncloud
.android
.files
.FileMenuFilter
;
50 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
51 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
52 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
53 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
54 import com
.owncloud
.android
.ui
.activity
.FolderPickerActivity
;
55 import com
.owncloud
.android
.ui
.activity
.OnEnforceableRefreshListener
;
56 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
57 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
58 import com
.owncloud
.android
.ui
.dialog
.FileActionsDialogFragment
;
59 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
60 import com
.owncloud
.android
.ui
.dialog
.RenameFileDialogFragment
;
61 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
62 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
63 import com
.owncloud
.android
.utils
.DialogMenuItem
;
64 import com
.owncloud
.android
.utils
.FileStorageUtils
;
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 FileFragment
.ContainerActivity mContainerActivity
;
85 private OCFile mFile
= null
;
86 private FileListListAdapter mAdapter
;
87 private boolean mJustFolders
;
89 private OCFile mTargetFile
;
97 public void onAttach(Activity activity
) {
98 super.onAttach(activity
);
99 Log_OC
.e(TAG
, "onAttach");
101 mContainerActivity
= (FileFragment
.ContainerActivity
) activity
;
103 } catch (ClassCastException e
) {
104 throw new ClassCastException(activity
.toString() + " must implement " +
105 FileFragment
.ContainerActivity
.class.getSimpleName());
108 setOnRefreshListener((OnEnforceableRefreshListener
) activity
);
110 } catch (ClassCastException e
) {
111 throw new ClassCastException(activity
.toString() + " must implement " +
112 SwipeRefreshLayout
.OnRefreshListener
.class.getSimpleName());
120 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
121 Log_OC
.i(TAG
, "onCreateView() start");
122 View v
= super.onCreateView(inflater
, container
, savedInstanceState
);
124 // Setup FAB listeners
125 getFabUpload().setOnClickListener(new View
.OnClickListener() {
127 public void onClick(View v
) {
128 ((FileDisplayActivity
)getActivity()).uploadLocalFilesSelected();
129 getFabMain().collapse();
133 getFabMkdir().setOnClickListener(new View
.OnClickListener() {
135 public void onClick(View v
) {
136 ((FileDisplayActivity
) getActivity()).createFolder();
137 getFabMain().collapse();
141 getFabUploadFromApp().setOnClickListener(new View
.OnClickListener() {
143 public void onClick(View v
) {
144 ((FileDisplayActivity
) getActivity()).uploadFromOtherAppsSelected();
145 getFabMain().collapse();
149 Log_OC
.i(TAG
, "onCreateView() end");
155 public void onDetach() {
156 setOnRefreshListener(null
);
157 mContainerActivity
= null
;
165 public void onActivityCreated(Bundle savedInstanceState
) {
166 super.onActivityCreated(savedInstanceState
);
167 Log_OC
.e(TAG
, "onActivityCreated() start");
169 if (savedInstanceState
!= null
) {
170 mFile
= savedInstanceState
.getParcelable(KEY_FILE
);
174 setFooterEnabled(false
);
176 setFooterEnabled(true
);
179 Bundle args
= getArguments();
180 mJustFolders
= (args
== null
) ? false
: args
.getBoolean(ARG_JUST_FOLDERS
, false
);
181 mAdapter
= new FileListListAdapter(
186 setListAdapter(mAdapter
);
188 registerLongClickListener();
191 private void registerLongClickListener() {
192 getListView().setOnItemLongClickListener(new AdapterView
.OnItemLongClickListener() {
193 public boolean onItemLongClick(AdapterView
<?
> arg0
, View v
,
194 int index
, long arg3
) {
195 showFileAction(index
);
202 private void showFileAction(int fileIndex
) {
203 Bundle args
= getArguments();
204 PopupMenu pm
= new PopupMenu(getActivity(),null
);
205 Menu menu
= pm
.getMenu();
207 boolean allowContextualActions
=
208 (args
== null
) ? true
: args
.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS
, true
);
210 if (allowContextualActions
) {
211 MenuInflater inflater
= getActivity().getMenuInflater();
213 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
214 OCFile targetFile
= (OCFile
) mAdapter
.getItem(fileIndex
);
216 if (mContainerActivity
.getStorageManager() != null
) {
217 FileMenuFilter mf
= new FileMenuFilter(
219 mContainerActivity
.getStorageManager().getAccount(),
226 /// TODO break this direct dependency on FileDisplayActivity... if possible
227 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
228 FileFragment frag
= ((FileDisplayActivity
)getActivity()).getSecondFragment();
229 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
230 frag
.getFile().getFileId() == targetFile
.getFileId()) {
231 item
= menu
.findItem(R
.id
.action_see_details
);
233 item
.setVisible(false
);
234 item
.setEnabled(false
);
238 MenuItem menuItem
= new DialogMenuItem(item
.getItemId());
239 FileActionsDialogFragment dialog
= FileActionsDialogFragment
.newInstance(menu
, fileIndex
);
240 dialog
.setTargetFragment(this, 0);
241 dialog
.show(getFragmentManager(), FileActionsDialogFragment
.FTAG_FILE_ACTIONS
);
246 * Saves the current listed folder.
249 public void onSaveInstanceState (Bundle outState
) {
250 super.onSaveInstanceState(outState
);
251 outState
.putParcelable(KEY_FILE
, mFile
);
255 * Call this, when the user presses the up button.
257 * Tries to move up the current folder one level. If the parent folder was removed from the
258 * database, it continues browsing up until finding an existing folders.
260 * return Count of folder levels browsed up.
262 public int onBrowseUp() {
263 OCFile parentDir
= null
;
267 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
269 String parentPath
= null
;
270 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
271 parentPath
= new File(mFile
.getRemotePath()).getParent();
272 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
273 parentPath
+ OCFile
.PATH_SEPARATOR
;
274 parentDir
= storageManager
.getFileByPath(parentPath
);
277 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
);
279 while (parentDir
== null
) {
280 parentPath
= new File(parentPath
).getParent();
281 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
282 parentPath
+ OCFile
.PATH_SEPARATOR
;
283 parentDir
= storageManager
.getFileByPath(parentPath
);
285 } // exit is granted because storageManager.getFileByPath("/") never returns null
288 // TODO Enable when "On Device" is recovered ?
289 listDirectory(mFile
/*, MainApp.getOnlyOnDevice()*/);
293 // restore index and top position
294 restoreIndexAndTopPosition();
296 } // else - should never happen now
302 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
303 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
305 if (file
.isFolder()) {
306 // update state and view of this fragment
307 // TODO Enable when "On Device" is recovered ?
308 listDirectory(file
/*, MainApp.getOnlyOnDevice()*/);
309 // then, notify parent activity to let it update its state and view
310 mContainerActivity
.onBrowsedDownTo(file
);
311 // save index and top position
312 saveIndexAndTopPosition(position
);
314 } else { /// Click on a file
315 if (PreviewImageFragment
.canBePreviewed(file
)) {
316 // preview image - it handles the download, if needed
317 ((FileDisplayActivity
)mContainerActivity
).startImagePreview(file
);
319 } else if (file
.isDown()) {
320 if (PreviewMediaFragment
.canBePreviewed(file
)) {
322 ((FileDisplayActivity
)mContainerActivity
).startMediaPreview(file
, 0, true
);
324 mContainerActivity
.getFileOperationsHelper().openFile(file
);
328 // automatic download, preview on finish
329 ((FileDisplayActivity
)mContainerActivity
).startDownloadForPreview(file
);
335 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
344 public void onCreateContextMenu (
345 ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
346 Bundle args
= getArguments();
347 boolean allowContextualActions
=
348 (args
== null
) ? true
: args
.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS
, true
);
349 if (allowContextualActions
) {
350 MenuInflater inflater
= getActivity().getMenuInflater();
351 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
352 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
353 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
355 if (mContainerActivity
.getStorageManager() != null
) {
356 FileMenuFilter mf
= new FileMenuFilter(
358 mContainerActivity
.getStorageManager().getAccount(),
365 /// TODO break this direct dependency on FileDisplayActivity... if possible
366 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
367 FileFragment frag
= ((FileDisplayActivity
)getActivity()).getSecondFragment();
368 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
369 frag
.getFile().getFileId() == targetFile
.getFileId()) {
370 item
= menu
.findItem(R
.id
.action_see_details
);
372 item
.setVisible(false
);
373 item
.setEnabled(false
);
383 public boolean onFileActionChosen(int menuId
, int filePosition
) {
384 mTargetFile
= (OCFile
) mAdapter
.getItem(filePosition
);
386 case R
.id
.action_share_file
: {
387 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
390 case R
.id
.action_open_file_with
: {
391 mContainerActivity
.getFileOperationsHelper().openFile(mTargetFile
);
394 case R
.id
.action_unshare_file
: {
395 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
398 case R
.id
.action_rename_file
: {
399 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(mTargetFile
);
400 dialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_RENAME_FILE
);
403 case R
.id
.action_remove_file
: {
404 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(mTargetFile
);
405 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
408 case R
.id
.action_download_file
:
409 case R
.id
.action_sync_file
: {
410 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
413 case R
.id
.action_cancel_download
:
414 case R
.id
.action_cancel_upload
: {
415 ((FileDisplayActivity
) mContainerActivity
).cancelTransference(mTargetFile
);
418 case R
.id
.action_see_details
: {
419 mContainerActivity
.showDetails(mTargetFile
);
422 case R
.id
.action_send_file
: {
424 if (!mTargetFile
.isDown()) { // Download the file
425 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
426 ((FileDisplayActivity
) mContainerActivity
).startDownloadForSending(mTargetFile
);
429 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(mTargetFile
);
433 case R
.id
.action_move
: {
434 Intent action
= new Intent(getActivity(), FolderPickerActivity
.class);
436 // Pass mTargetFile that contains info of selected file/folder
437 action
.putExtra(FolderPickerActivity
.EXTRA_FILE
, mTargetFile
);
438 getActivity().startActivityForResult(action
, FileDisplayActivity
.ACTION_MOVE_FILES
);
441 case R
.id
.action_favorite_file
: {
442 mContainerActivity
.getFileOperationsHelper().toggleFavorite(mTargetFile
, true
);
445 case R
.id
.action_unfavorite_file
: {
446 mContainerActivity
.getFileOperationsHelper().toggleFavorite(mTargetFile
, false
);
458 public boolean onContextItemSelected (MenuItem item
) {
459 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
460 boolean matched
= onFileActionChosen(item
.getItemId(), ((AdapterContextMenuInfo
) item
.getMenuInfo()).position
);
462 return super.onContextItemSelected(item
);
470 * Use this to query the {@link OCFile} that is currently
471 * being displayed by this fragment
472 * @return The currently viewed OCFile
474 public OCFile
getCurrentFile(){
479 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
481 public void listDirectory(/*boolean onlyOnDevice*/){
483 // TODO Enable when "On Device" is recovered ?
484 // listDirectory(null, onlyOnDevice);
487 public void refreshDirectory(){
488 // TODO Enable when "On Device" is recovered ?
489 listDirectory(getCurrentFile()/*, MainApp.getOnlyOnDevice()*/);
493 * Lists the given directory on the view. When the input parameter is null,
494 * it will either refresh the last known directory. list the root
495 * if there never was a directory.
497 * @param directory File to be listed
499 public void listDirectory(OCFile directory
/*, boolean onlyOnDevice*/) {
500 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
501 if (storageManager
!= null
) {
503 // Check input parameters for null
504 if(directory
== null
){
508 directory
= storageManager
.getFileByPath("/");
509 if (directory
== null
) return; // no files, wait for sync
514 // If that's not a directory -> List its parent
515 if(!directory
.isFolder()){
516 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
517 directory
= storageManager
.getFileById(directory
.getParentId());
520 // TODO Enable when "On Device" is recovered ?
521 mAdapter
.swapDirectory(directory
, storageManager
/*, onlyOnDevice*/);
522 if (mFile
== null
|| !mFile
.equals(directory
)) {
523 mCurrentListView
.setSelection(0);
532 private void updateLayout() {
534 int filesCount
= 0, foldersCount
= 0, imagesCount
= 0;
535 int count
= mAdapter
.getCount();
537 for (int i
=0; i
< count
; i
++) {
538 file
= (OCFile
) mAdapter
.getItem(i
);
539 if (file
.isFolder()) {
549 setFooterText(generateFooterText(filesCount
, foldersCount
));
551 // decide grid vs list view
552 OwnCloudVersion version
= AccountUtils
.getServerVersion(
553 ((FileActivity
)mContainerActivity
).getAccount());
554 if (version
!= null
&& version
.supportsRemoteThumbnails() &&
555 imagesCount
> 0 && imagesCount
== filesCount
) {
557 registerLongClickListener();
564 private String
generateFooterText(int filesCount
, int foldersCount
) {
566 if (filesCount
<= 0) {
567 if (foldersCount
<= 0) {
570 } else if (foldersCount
== 1) {
571 output
= getResources().getString(R
.string
.file_list__footer__folder
);
573 } else { // foldersCount > 1
574 output
= getResources().getString(R
.string
.file_list__footer__folders
, foldersCount
);
577 } else if (filesCount
== 1) {
578 if (foldersCount
<= 0) {
579 output
= getResources().getString(R
.string
.file_list__footer__file
);
581 } else if (foldersCount
== 1) {
582 output
= getResources().getString(R
.string
.file_list__footer__file_and_folder
);
584 } else { // foldersCount > 1
585 output
= getResources().getString(R
.string
.file_list__footer__file_and_folders
, foldersCount
);
587 } else { // filesCount > 1
588 if (foldersCount
<= 0) {
589 output
= getResources().getString(R
.string
.file_list__footer__files
, filesCount
);
591 } else if (foldersCount
== 1) {
592 output
= getResources().getString(R
.string
.file_list__footer__files_and_folder
, filesCount
);
594 } else { // foldersCount > 1
595 output
= getResources().getString(
596 R
.string
.file_list__footer__files_and_folders
, filesCount
, foldersCount
604 public void sortByName(boolean descending
) {
605 mAdapter
.setSortOrder(FileStorageUtils
.SORT_NAME
, descending
);
608 public void sortByDate(boolean descending
) {
609 mAdapter
.setSortOrder(FileStorageUtils
.SORT_DATE
, descending
);
612 public void sortBySize(boolean descending
) {
613 mAdapter
.setSortOrder(FileStorageUtils
.SORT_SIZE
, descending
);