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
.ContextMenu
;
30 import android
.view
.Menu
;
31 import android
.view
.MenuInflater
;
32 import android
.view
.MenuItem
;
33 import android
.view
.View
;
34 import android
.widget
.AdapterView
;
35 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
36 import android
.widget
.PopupMenu
;
38 import com
.owncloud
.android
.R
;
39 import com
.owncloud
.android
.authentication
.AccountUtils
;
40 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
41 import com
.owncloud
.android
.datamodel
.OCFile
;
42 import com
.owncloud
.android
.files
.FileMenuFilter
;
43 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
44 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
45 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
46 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
47 import com
.owncloud
.android
.ui
.activity
.FolderPickerActivity
;
48 import com
.owncloud
.android
.ui
.activity
.OnEnforceableRefreshListener
;
49 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
50 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
51 import com
.owncloud
.android
.ui
.dialog
.FileActionsDialogFragment
;
52 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
53 import com
.owncloud
.android
.ui
.dialog
.RenameFileDialogFragment
;
54 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
55 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
56 import com
.owncloud
.android
.utils
.DisplayUtils
;
57 import com
.owncloud
.android
.utils
.FileStorageUtils
;
58 import com
.owncloud
.android
.ui
.preview
.PreviewTextFragment
;
63 * A Fragment that lists all files and folders in a given path.
65 * TODO refactor to get rid of direct dependency on FileDisplayActivity
67 public class OCFileListFragment
extends ExtendedListFragment
implements FileActionsDialogFragment
.FileActionsDialogFragmentListener
{
69 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
71 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ?
72 OCFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
74 public final static String ARG_JUST_FOLDERS
= MY_PACKAGE
+ ".JUST_FOLDERS";
75 public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS
= MY_PACKAGE
+ ".ALLOW_CONTEXTUAL";
77 private static final String KEY_FILE
= MY_PACKAGE
+ ".extra.FILE";
79 private FileFragment
.ContainerActivity mContainerActivity
;
81 private OCFile mFile
= null
;
82 private FileListListAdapter mAdapter
;
83 private boolean mJustFolders
;
85 private OCFile mTargetFile
;
93 public void onAttach(Activity activity
) {
94 super.onAttach(activity
);
95 Log_OC
.e(TAG
, "onAttach");
97 mContainerActivity
= (FileFragment
.ContainerActivity
) activity
;
99 } catch (ClassCastException e
) {
100 throw new ClassCastException(activity
.toString() + " must implement " +
101 FileFragment
.ContainerActivity
.class.getSimpleName());
104 setOnRefreshListener((OnEnforceableRefreshListener
) activity
);
106 } catch (ClassCastException e
) {
107 throw new ClassCastException(activity
.toString() + " must implement " +
108 SwipeRefreshLayout
.OnRefreshListener
.class.getSimpleName());
114 public void onDetach() {
115 setOnRefreshListener(null
);
116 mContainerActivity
= null
;
124 public void onActivityCreated(Bundle savedInstanceState
) {
125 super.onActivityCreated(savedInstanceState
);
126 Log_OC
.e(TAG
, "onActivityCreated() start");
128 if (savedInstanceState
!= null
) {
129 mFile
= savedInstanceState
.getParcelable(KEY_FILE
);
133 setFooterEnabled(false
);
135 setFooterEnabled(true
);
138 Bundle args
= getArguments();
139 mJustFolders
= (args
== null
) ? false
: args
.getBoolean(ARG_JUST_FOLDERS
, false
);
140 mAdapter
= new FileListListAdapter(
145 setListAdapter(mAdapter
);
147 registerLongClickListener();
150 private void registerLongClickListener() {
151 getListView().setOnItemLongClickListener(new AdapterView
.OnItemLongClickListener() {
152 public boolean onItemLongClick(AdapterView
<?
> arg0
, View v
,
153 int index
, long arg3
) {
154 showFileAction(index
);
161 private void showFileAction(int fileIndex
) {
162 Bundle args
= getArguments();
163 PopupMenu pm
= new PopupMenu(getActivity(),null
);
164 Menu menu
= pm
.getMenu();
166 boolean allowContextualActions
=
167 (args
== null
) ? true
: args
.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS
, true
);
169 if (allowContextualActions
) {
170 MenuInflater inflater
= getActivity().getMenuInflater();
172 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
173 OCFile targetFile
= (OCFile
) mAdapter
.getItem(fileIndex
);
175 if (mContainerActivity
.getStorageManager() != null
) {
176 FileMenuFilter mf
= new FileMenuFilter(
178 mContainerActivity
.getStorageManager().getAccount(),
185 /// TODO break this direct dependency on FileDisplayActivity... if possible
186 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
187 FileFragment frag
= ((FileDisplayActivity
)getActivity()).getSecondFragment();
188 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
189 frag
.getFile().getFileId() == targetFile
.getFileId()) {
190 item
= menu
.findItem(R
.id
.action_see_details
);
192 item
.setVisible(false
);
193 item
.setEnabled(false
);
197 FileActionsDialogFragment dialog
= FileActionsDialogFragment
.newInstance(menu
, fileIndex
, targetFile
.getFileName());
198 dialog
.setTargetFragment(this, 0);
199 dialog
.show(getFragmentManager(), FileActionsDialogFragment
.FTAG_FILE_ACTIONS
);
204 * Saves the current listed folder.
207 public void onSaveInstanceState(Bundle outState
) {
208 super.onSaveInstanceState(outState
);
209 outState
.putParcelable(KEY_FILE
, mFile
);
213 * Call this, when the user presses the up button.
215 * Tries to move up the current folder one level. If the parent folder was removed from the
216 * database, it continues browsing up until finding an existing folders.
218 * return Count of folder levels browsed up.
220 public int onBrowseUp() {
221 OCFile parentDir
= null
;
225 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
227 String parentPath
= null
;
228 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
229 parentPath
= new File(mFile
.getRemotePath()).getParent();
230 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
231 parentPath
+ OCFile
.PATH_SEPARATOR
;
232 parentDir
= storageManager
.getFileByPath(parentPath
);
235 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
);
237 while (parentDir
== null
) {
238 parentPath
= new File(parentPath
).getParent();
239 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
240 parentPath
+ OCFile
.PATH_SEPARATOR
;
241 parentDir
= storageManager
.getFileByPath(parentPath
);
243 } // exit is granted because storageManager.getFileByPath("/") never returns null
246 // TODO Enable when "On Device" is recovered ?
247 listDirectory(mFile
/*, MainApp.getOnlyOnDevice()*/);
251 // restore index and top position
252 restoreIndexAndTopPosition();
254 } // else - should never happen now
260 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
261 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
263 if (file
.isFolder()) {
264 // update state and view of this fragment
265 // TODO Enable when "On Device" is recovered ?
266 listDirectory(file
/*, MainApp.getOnlyOnDevice()*/);
267 // then, notify parent activity to let it update its state and view
268 mContainerActivity
.onBrowsedDownTo(file
);
269 // save index and top position
270 saveIndexAndTopPosition(position
);
272 } else { /// Click on a file
273 if (PreviewImageFragment
.canBePreviewed(file
)) {
274 // preview image - it handles the download, if needed
275 ((FileDisplayActivity
)mContainerActivity
).startImagePreview(file
);
276 } else if (PreviewTextFragment
.canBePreviewed(file
)){
277 ((FileDisplayActivity
)mContainerActivity
).startTextPreview(file
);
278 } else if (file
.isDown()) {
279 if (PreviewMediaFragment
.canBePreviewed(file
)) {
281 ((FileDisplayActivity
) mContainerActivity
).startMediaPreview(file
, 0, true
);
283 mContainerActivity
.getFileOperationsHelper().openFile(file
);
287 // automatic download, preview on finish
288 ((FileDisplayActivity
) mContainerActivity
).startDownloadForPreview(file
);
294 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
303 public void onCreateContextMenu(
304 ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
305 Bundle args
= getArguments();
306 boolean allowContextualActions
=
307 (args
== null
) ? true
: args
.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS
, true
);
308 if (allowContextualActions
) {
309 MenuInflater inflater
= getActivity().getMenuInflater();
310 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
311 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
312 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
314 if (mContainerActivity
.getStorageManager() != null
) {
315 FileMenuFilter mf
= new FileMenuFilter(
317 mContainerActivity
.getStorageManager().getAccount(),
324 /// TODO break this direct dependency on FileDisplayActivity... if possible
325 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
326 FileFragment frag
= ((FileDisplayActivity
)getActivity()).getSecondFragment();
327 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
328 frag
.getFile().getFileId() == targetFile
.getFileId()) {
329 item
= menu
.findItem(R
.id
.action_see_details
);
331 item
.setVisible(false
);
332 item
.setEnabled(false
);
336 // String.format(mContext.getString(R.string.subject_token),
337 // getClient().getCredentials().getUsername(), file.getFileName()));
345 public boolean onFileActionChosen(int menuId
, int filePosition
) {
346 mTargetFile
= (OCFile
) mAdapter
.getItem(filePosition
);
348 case R
.id
.action_share_file
: {
349 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
352 case R
.id
.action_open_file_with
: {
353 mContainerActivity
.getFileOperationsHelper().openFile(mTargetFile
);
356 case R
.id
.action_unshare_file
: {
357 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
360 case R
.id
.action_rename_file
: {
361 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(mTargetFile
);
362 dialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_RENAME_FILE
);
365 case R
.id
.action_remove_file
: {
366 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(mTargetFile
);
367 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
370 case R
.id
.action_download_file
:
371 case R
.id
.action_sync_file
: {
372 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
375 case R
.id
.action_cancel_download
:
376 case R
.id
.action_cancel_upload
: {
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_move
: {
396 Intent action
= new Intent(getActivity(), FolderPickerActivity
.class);
398 // Pass mTargetFile that contains info of selected file/folder
399 action
.putExtra(FolderPickerActivity
.EXTRA_FILE
, mTargetFile
);
400 getActivity().startActivityForResult(action
, FileDisplayActivity
.ACTION_MOVE_FILES
);
403 case R
.id
.action_favorite_file
: {
404 mContainerActivity
.getFileOperationsHelper().toggleFavorite(mTargetFile
, true
);
407 case R
.id
.action_unfavorite_file
: {
408 mContainerActivity
.getFileOperationsHelper().toggleFavorite(mTargetFile
, false
);
411 case R
.id
.action_copy
:
412 Intent action
= new Intent(getActivity(), FolderPickerActivity
.class);
414 // Pass mTargetFile that contains info of selected file/folder
415 action
.putExtra(FolderPickerActivity
.EXTRA_FILE
, mTargetFile
);
416 getActivity().startActivityForResult(action
, FileDisplayActivity
.ACTION_COPY_FILES
);
427 public boolean onContextItemSelected (MenuItem item
) {
428 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
429 boolean matched
= onFileActionChosen(item
.getItemId(), ((AdapterContextMenuInfo
) item
.getMenuInfo()).position
);
431 return super.onContextItemSelected(item
);
439 * Use this to query the {@link OCFile} that is currently
440 * being displayed by this fragment
442 * @return The currently viewed OCFile
444 public OCFile
getCurrentFile() {
449 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
451 public void listDirectory(/*boolean onlyOnDevice*/){
453 // TODO Enable when "On Device" is recovered ?
454 // listDirectory(null, onlyOnDevice);
457 public void refreshDirectory(){
458 // TODO Enable when "On Device" is recovered ?
459 listDirectory(getCurrentFile()/*, MainApp.getOnlyOnDevice()*/);
463 * Lists the given directory on the view. When the input parameter is null,
464 * it will either refresh the last known directory. list the root
465 * if there never was a directory.
467 * @param directory File to be listed
469 public void listDirectory(OCFile directory
/*, boolean onlyOnDevice*/) {
470 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
471 if (storageManager
!= null
) {
473 // Check input parameters for null
474 if (directory
== null
) {
478 directory
= storageManager
.getFileByPath("/");
479 if (directory
== null
) return; // no files, wait for sync
484 // If that's not a directory -> List its parent
485 if (!directory
.isFolder()) {
486 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
487 directory
= storageManager
.getFileById(directory
.getParentId());
490 // TODO Enable when "On Device" is recovered ?
491 mAdapter
.swapDirectory(directory
, storageManager
/*, onlyOnDevice*/);
492 if (mFile
== null
|| !mFile
.equals(directory
)) {
493 mCurrentListView
.setSelection(0);
502 private void updateLayout() {
504 int filesCount
= 0, foldersCount
= 0, imagesCount
= 0;
505 int count
= mAdapter
.getCount();
507 for (int i
=0; i
< count
; i
++) {
508 file
= (OCFile
) mAdapter
.getItem(i
);
509 if (file
.isFolder()) {
512 if (!file
.isHidden()) {
515 if (file
.isImage()) {
522 setFooterText(generateFooterText(filesCount
, foldersCount
));
524 // decide grid vs list view
525 OwnCloudVersion version
= AccountUtils
.getServerVersion(
526 ((FileActivity
)mContainerActivity
).getAccount());
527 if (version
!= null
&& version
.supportsRemoteThumbnails() &&
528 DisplayUtils
.isGridView(mFile
, mContainerActivity
.getStorageManager())) {
530 registerLongClickListener();
537 private String
generateFooterText(int filesCount
, int foldersCount
) {
539 if (filesCount
<= 0) {
540 if (foldersCount
<= 0) {
543 } else if (foldersCount
== 1) {
544 output
= getResources().getString(R
.string
.file_list__footer__folder
);
546 } else { // foldersCount > 1
547 output
= getResources().getString(R
.string
.file_list__footer__folders
, foldersCount
);
550 } else if (filesCount
== 1) {
551 if (foldersCount
<= 0) {
552 output
= getResources().getString(R
.string
.file_list__footer__file
);
554 } else if (foldersCount
== 1) {
555 output
= getResources().getString(R
.string
.file_list__footer__file_and_folder
);
557 } else { // foldersCount > 1
558 output
= getResources().getString(R
.string
.file_list__footer__file_and_folders
, foldersCount
);
560 } else { // filesCount > 1
561 if (foldersCount
<= 0) {
562 output
= getResources().getString(R
.string
.file_list__footer__files
, filesCount
);
564 } else if (foldersCount
== 1) {
565 output
= getResources().getString(R
.string
.file_list__footer__files_and_folder
, filesCount
);
567 } else { // foldersCount > 1
568 output
= getResources().getString(
569 R
.string
.file_list__footer__files_and_folders
, filesCount
, foldersCount
577 public void sortByName(boolean descending
) {
578 mAdapter
.setSortOrder(FileStorageUtils
.SORT_NAME
, descending
);
581 public void sortByDate(boolean descending
) {
582 mAdapter
.setSortOrder(FileStorageUtils
.SORT_DATE
, descending
);
585 public void sortBySize(boolean descending
) {
586 mAdapter
.setSortOrder(FileStorageUtils
.SORT_SIZE
, descending
);