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
.accounts
.Account
;
26 import android
.app
.Activity
;
27 import android
.content
.Context
;
28 import android
.content
.DialogInterface
;
29 import android
.content
.Intent
;
30 import android
.net
.Uri
;
31 import android
.os
.Bundle
;
32 import android
.support
.v4
.widget
.SwipeRefreshLayout
;
33 import android
.support
.v7
.app
.AlertDialog
;
34 import android
.view
.ContextMenu
;
35 import android
.view
.Menu
;
36 import android
.view
.MenuInflater
;
37 import android
.view
.MenuItem
;
38 import android
.view
.View
;
39 import android
.widget
.AdapterView
;
40 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
41 import android
.widget
.PopupMenu
;
43 import com
.owncloud
.android
.MainApp
;
44 import com
.owncloud
.android
.R
;
45 import com
.owncloud
.android
.authentication
.AccountUtils
;
46 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
47 import com
.owncloud
.android
.datamodel
.OCFile
;
48 import com
.owncloud
.android
.files
.FileMenuFilter
;
49 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
50 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
51 import com
.owncloud
.android
.media
.MediaService
;
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
.ui
.preview
.PreviewTextFragment
;
64 import com
.owncloud
.android
.utils
.FileStorageUtils
;
69 * A Fragment that lists all files and folders in a given path.
71 * TODO refactor to get rid of direct dependency on FileDisplayActivity
73 public class OCFileListFragment
extends ExtendedListFragment
implements FileActionsDialogFragment
.FileActionsDialogFragmentListener
{
75 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
77 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ?
78 OCFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
80 public final static String ARG_JUST_FOLDERS
= MY_PACKAGE
+ ".JUST_FOLDERS";
81 public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS
= MY_PACKAGE
+ ".ALLOW_CONTEXTUAL";
83 private static final String KEY_FILE
= MY_PACKAGE
+ ".extra.FILE";
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();
156 private void registerLongClickListener() {
157 getListView().setOnItemLongClickListener(new AdapterView
.OnItemLongClickListener() {
158 public boolean onItemLongClick(AdapterView
<?
> arg0
, View v
,
159 int index
, long arg3
) {
160 showFileAction(index
);
167 private void showFileAction(int fileIndex
) {
168 Bundle args
= getArguments();
169 PopupMenu pm
= new PopupMenu(getActivity(),null
);
170 Menu menu
= pm
.getMenu();
172 boolean allowContextualActions
=
173 (args
== null
) ? true
: args
.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS
, true
);
175 if (allowContextualActions
) {
176 MenuInflater inflater
= getActivity().getMenuInflater();
178 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
179 OCFile targetFile
= (OCFile
) mAdapter
.getItem(fileIndex
);
181 if (mContainerActivity
.getStorageManager() != null
) {
182 FileMenuFilter mf
= new FileMenuFilter(
184 mContainerActivity
.getStorageManager().getAccount(),
191 /// TODO break this direct dependency on FileDisplayActivity... if possible
192 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
193 FileFragment frag
= ((FileDisplayActivity
)getActivity()).getSecondFragment();
194 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
195 frag
.getFile().getFileId() == targetFile
.getFileId()) {
196 item
= menu
.findItem(R
.id
.action_see_details
);
198 item
.setVisible(false
);
199 item
.setEnabled(false
);
203 FileActionsDialogFragment dialog
= FileActionsDialogFragment
.newInstance(menu
, fileIndex
, targetFile
.getFileName());
204 dialog
.setTargetFragment(this, 0);
205 dialog
.show(getFragmentManager(), FileActionsDialogFragment
.FTAG_FILE_ACTIONS
);
210 * Saves the current listed folder.
213 public void onSaveInstanceState(Bundle outState
) {
214 super.onSaveInstanceState(outState
);
215 outState
.putParcelable(KEY_FILE
, mFile
);
219 * Call this, when the user presses the up button.
221 * Tries to move up the current folder one level. If the parent folder was removed from the
222 * database, it continues browsing up until finding an existing folders.
224 * return Count of folder levels browsed up.
226 public int onBrowseUp() {
227 OCFile parentDir
= null
;
231 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
233 String parentPath
= null
;
234 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
235 parentPath
= new File(mFile
.getRemotePath()).getParent();
236 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
237 parentPath
+ OCFile
.PATH_SEPARATOR
;
238 parentDir
= storageManager
.getFileByPath(parentPath
);
241 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
);
243 while (parentDir
== null
) {
244 parentPath
= new File(parentPath
).getParent();
245 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
246 parentPath
+ OCFile
.PATH_SEPARATOR
;
247 parentDir
= storageManager
.getFileByPath(parentPath
);
249 } // exit is granted because storageManager.getFileByPath("/") never returns null
252 // TODO Enable when "On Device" is recovered ?
253 listDirectory(mFile
/*, MainApp.getOnlyOnDevice()*/);
257 // restore index and top position
258 restoreIndexAndTopPosition();
260 } // else - should never happen now
266 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
267 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
269 if (file
.isFolder()) {
270 // update state and view of this fragment
271 // TODO Enable when "On Device" is recovered ?
272 listDirectory(file
/*, MainApp.getOnlyOnDevice()*/);
273 // then, notify parent activity to let it update its state and view
274 mContainerActivity
.onBrowsedDownTo(file
);
275 // save index and top position
276 saveIndexAndTopPosition(position
);
278 } else { /// Click on a file
279 if (PreviewImageFragment
.canBePreviewed(file
)) {
280 // preview image - it handles the download, if needed
281 ((FileDisplayActivity
)mContainerActivity
).startImagePreview(file
);
282 } else if (PreviewTextFragment
.canBePreviewed(file
)){
283 ((FileDisplayActivity
)mContainerActivity
).startTextPreview(file
);
284 } else if (PreviewMediaFragment
.canBePreviewed(file
)) {
286 ((FileDisplayActivity
) mContainerActivity
).startMediaPreview(file
, 0, true
);
287 } else if (file
.isDown()) {
288 mContainerActivity
.getFileOperationsHelper().openFile(file
);
291 // automatic download, preview on finish
292 ((FileDisplayActivity
) mContainerActivity
).startDownloadForPreview(file
);
298 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
307 public void onCreateContextMenu(
308 ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
309 Bundle args
= getArguments();
310 boolean allowContextualActions
=
311 (args
== null
) ? true
: args
.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS
, true
);
312 if (allowContextualActions
) {
313 MenuInflater inflater
= getActivity().getMenuInflater();
314 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
315 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
316 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
318 if (mContainerActivity
.getStorageManager() != null
) {
319 FileMenuFilter mf
= new FileMenuFilter(
321 mContainerActivity
.getStorageManager().getAccount(),
328 /// TODO break this direct dependency on FileDisplayActivity... if possible
329 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
330 FileFragment frag
= ((FileDisplayActivity
)getActivity()).getSecondFragment();
331 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
332 frag
.getFile().getFileId() == targetFile
.getFileId()) {
333 item
= menu
.findItem(R
.id
.action_see_details
);
335 item
.setVisible(false
);
336 item
.setEnabled(false
);
346 public boolean onFileActionChosen(int menuId
, int filePosition
) {
347 mTargetFile
= (OCFile
) mAdapter
.getItem(filePosition
);
349 case R
.id
.action_share_file
: {
350 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
353 case R
.id
.action_open_file_with
: {
354 mContainerActivity
.getFileOperationsHelper().openFile(mTargetFile
);
357 case R
.id
.action_unshare_file
: {
358 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
361 case R
.id
.action_rename_file
: {
362 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(mTargetFile
);
363 dialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_RENAME_FILE
);
366 case R
.id
.action_remove_file
: {
367 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(mTargetFile
);
368 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
371 case R
.id
.action_download_file
:
372 case R
.id
.action_sync_file
: {
373 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
376 case R
.id
.action_cancel_sync
: {
377 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(mTargetFile
);
380 case R
.id
.action_see_details
: {
381 mContainerActivity
.showDetails(mTargetFile
);
384 case R
.id
.action_send_file
: {
386 if (!mTargetFile
.isDown()) { // Download the file
387 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
388 ((FileDisplayActivity
) mContainerActivity
).startDownloadForSending(mTargetFile
);
391 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(mTargetFile
);
395 case R
.id
.action_stream_file
: {
396 Account account
= ((FileActivity
)mContainerActivity
).getAccount();
397 Context context
= MainApp
.getAppContext();
398 String uri
= PreviewMediaFragment
.generateUrlWithCredentials(account
, context
, mTargetFile
);
399 MediaService
.streamWithExternalApp(uri
, getActivity()).show();
403 case R
.id
.action_move
: {
404 Intent action
= new Intent(getActivity(), FolderPickerActivity
.class);
406 // Pass mTargetFile that contains info of selected file/folder
407 action
.putExtra(FolderPickerActivity
.EXTRA_FILE
, mTargetFile
);
408 getActivity().startActivityForResult(action
, FileDisplayActivity
.ACTION_MOVE_FILES
);
411 case R
.id
.action_favorite_file
: {
412 mContainerActivity
.getFileOperationsHelper().toggleFavorite(mTargetFile
, true
);
415 case R
.id
.action_unfavorite_file
: {
416 mContainerActivity
.getFileOperationsHelper().toggleFavorite(mTargetFile
, false
);
419 case R
.id
.action_copy
:
420 Intent action
= new Intent(getActivity(), FolderPickerActivity
.class);
422 // Pass mTargetFile that contains info of selected file/folder
423 action
.putExtra(FolderPickerActivity
.EXTRA_FILE
, mTargetFile
);
424 getActivity().startActivityForResult(action
, FileDisplayActivity
.ACTION_COPY_FILES
);
435 public boolean onContextItemSelected (MenuItem item
) {
436 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
437 boolean matched
= onFileActionChosen(item
.getItemId(), ((AdapterContextMenuInfo
) item
.getMenuInfo()).position
);
439 return super.onContextItemSelected(item
);
447 * Use this to query the {@link OCFile} that is currently
448 * being displayed by this fragment
450 * @return The currently viewed OCFile
452 public OCFile
getCurrentFile() {
457 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
459 public void listDirectory(/*boolean onlyOnDevice*/){
461 // TODO Enable when "On Device" is recovered ?
462 // listDirectory(null, onlyOnDevice);
465 public void refreshDirectory(){
466 // TODO Enable when "On Device" is recovered ?
467 listDirectory(getCurrentFile()/*, MainApp.getOnlyOnDevice()*/);
471 * Lists the given directory on the view. When the input parameter is null,
472 * it will either refresh the last known directory. list the root
473 * if there never was a directory.
475 * @param directory File to be listed
477 public void listDirectory(OCFile directory
/*, boolean onlyOnDevice*/) {
478 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
479 if (storageManager
!= null
) {
481 // Check input parameters for null
482 if (directory
== null
) {
486 directory
= storageManager
.getFileByPath("/");
487 if (directory
== null
) return; // no files, wait for sync
492 // If that's not a directory -> List its parent
493 if (!directory
.isFolder()) {
494 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
495 directory
= storageManager
.getFileById(directory
.getParentId());
498 // TODO Enable when "On Device" is recovered ?
499 mAdapter
.swapDirectory(directory
, storageManager
/*, onlyOnDevice*/);
500 if (mFile
== null
|| !mFile
.equals(directory
)) {
501 mCurrentListView
.setSelection(0);
510 private void updateLayout() {
512 int filesCount
= 0, foldersCount
= 0, imagesCount
= 0;
513 int count
= mAdapter
.getCount();
515 for (int i
=0; i
< count
; i
++) {
516 file
= (OCFile
) mAdapter
.getItem(i
);
517 if (file
.isFolder()) {
520 if (!file
.isHidden()) {
523 if (file
.isImage()) {
530 setFooterText(generateFooterText(filesCount
, foldersCount
));
532 // decide grid vs list view
533 OwnCloudVersion version
= AccountUtils
.getServerVersion(
534 ((FileActivity
)mContainerActivity
).getAccount());
535 if (version
!= null
&& version
.supportsRemoteThumbnails() &&
536 imagesCount
> 0 && imagesCount
== filesCount
) {
538 registerLongClickListener();
545 private String
generateFooterText(int filesCount
, int foldersCount
) {
547 if (filesCount
<= 0) {
548 if (foldersCount
<= 0) {
551 } else if (foldersCount
== 1) {
552 output
= getResources().getString(R
.string
.file_list__footer__folder
);
554 } else { // foldersCount > 1
555 output
= getResources().getString(R
.string
.file_list__footer__folders
, foldersCount
);
558 } else if (filesCount
== 1) {
559 if (foldersCount
<= 0) {
560 output
= getResources().getString(R
.string
.file_list__footer__file
);
562 } else if (foldersCount
== 1) {
563 output
= getResources().getString(R
.string
.file_list__footer__file_and_folder
);
565 } else { // foldersCount > 1
566 output
= getResources().getString(R
.string
.file_list__footer__file_and_folders
, foldersCount
);
568 } else { // filesCount > 1
569 if (foldersCount
<= 0) {
570 output
= getResources().getString(R
.string
.file_list__footer__files
, filesCount
);
572 } else if (foldersCount
== 1) {
573 output
= getResources().getString(R
.string
.file_list__footer__files_and_folder
, filesCount
);
575 } else { // foldersCount > 1
576 output
= getResources().getString(
577 R
.string
.file_list__footer__files_and_folders
, filesCount
, foldersCount
585 public void sortByName(boolean descending
) {
586 mAdapter
.setSortOrder(FileStorageUtils
.SORT_NAME
, descending
);
589 public void sortByDate(boolean descending
) {
590 mAdapter
.setSortOrder(FileStorageUtils
.SORT_DATE
, descending
);
593 public void sortBySize(boolean descending
) {
594 mAdapter
.setSortOrder(FileStorageUtils
.SORT_SIZE
, descending
);