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
.MenuInflater
;
33 import android
.view
.MenuItem
;
34 import android
.view
.View
;
35 import android
.widget
.AdapterView
;
36 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
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
.services
.observer
.FileObserverService
;
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
.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
.FileStorageUtils
;
59 * A Fragment that lists all files and folders in a given path.
61 * TODO refactor to get rid of direct dependency on FileDisplayActivity
63 public class OCFileListFragment
extends ExtendedListFragment
{
65 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
67 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ?
68 OCFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
70 public final static String ARG_JUST_FOLDERS
= MY_PACKAGE
+ ".JUST_FOLDERS";
71 public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS
= MY_PACKAGE
+ ".ALLOW_CONTEXTUAL";
73 private static final String KEY_FILE
= MY_PACKAGE
+ ".extra.FILE";
75 private FileFragment
.ContainerActivity mContainerActivity
;
77 private OCFile mFile
= null
;
78 private FileListListAdapter mAdapter
;
79 private boolean mJustFolders
;
81 private OCFile mTargetFile
;
89 public void onAttach(Activity activity
) {
90 super.onAttach(activity
);
91 Log_OC
.e(TAG
, "onAttach");
93 mContainerActivity
= (FileFragment
.ContainerActivity
) activity
;
95 } catch (ClassCastException e
) {
96 throw new ClassCastException(activity
.toString() + " must implement " +
97 FileFragment
.ContainerActivity
.class.getSimpleName());
100 setOnRefreshListener((OnEnforceableRefreshListener
) activity
);
102 } catch (ClassCastException e
) {
103 throw new ClassCastException(activity
.toString() + " must implement " +
104 SwipeRefreshLayout
.OnRefreshListener
.class.getSimpleName());
110 public void onDetach() {
111 setOnRefreshListener(null
);
112 mContainerActivity
= null
;
120 public void onActivityCreated(Bundle savedInstanceState
) {
121 super.onActivityCreated(savedInstanceState
);
122 Log_OC
.e(TAG
, "onActivityCreated() start");
124 if (savedInstanceState
!= null
) {
125 mFile
= savedInstanceState
.getParcelable(KEY_FILE
);
129 setFooterEnabled(false
);
131 setFooterEnabled(true
);
134 Bundle args
= getArguments();
135 mJustFolders
= (args
== null
) ? false
: args
.getBoolean(ARG_JUST_FOLDERS
, false
);
136 mAdapter
= new FileListListAdapter(
141 setListAdapter(mAdapter
);
143 registerForContextMenu();
147 * Saves the current listed folder.
150 public void onSaveInstanceState (Bundle outState
) {
151 super.onSaveInstanceState(outState
);
152 outState
.putParcelable(KEY_FILE
, mFile
);
156 * Call this, when the user presses the up button.
158 * Tries to move up the current folder one level. If the parent folder was removed from the
159 * database, it continues browsing up until finding an existing folders.
161 * return Count of folder levels browsed up.
163 public int onBrowseUp() {
164 OCFile parentDir
= null
;
168 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
170 String parentPath
= null
;
171 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
172 parentPath
= new File(mFile
.getRemotePath()).getParent();
173 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
174 parentPath
+ OCFile
.PATH_SEPARATOR
;
175 parentDir
= storageManager
.getFileByPath(parentPath
);
178 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
);
180 while (parentDir
== null
) {
181 parentPath
= new File(parentPath
).getParent();
182 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
183 parentPath
+ OCFile
.PATH_SEPARATOR
;
184 parentDir
= storageManager
.getFileByPath(parentPath
);
186 } // exit is granted because storageManager.getFileByPath("/") never returns null
189 // TODO Enable when "On Device" is recovered ?
190 listDirectory(mFile
/*, MainApp.getOnlyOnDevice()*/);
194 // restore index and top position
195 restoreIndexAndTopPosition();
197 } // else - should never happen now
203 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
204 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
206 if (file
.isFolder()) {
207 // update state and view of this fragment
208 // TODO Enable when "On Device" is recovered ?
209 listDirectory(file
/*, MainApp.getOnlyOnDevice()*/);
210 // then, notify parent activity to let it update its state and view
211 mContainerActivity
.onBrowsedDownTo(file
);
212 // save index and top position
213 saveIndexAndTopPosition(position
);
215 } else { /// Click on a file
216 if (PreviewImageFragment
.canBePreviewed(file
)) {
217 // preview image - it handles the download, if needed
218 ((FileDisplayActivity
)mContainerActivity
).startImagePreview(file
);
220 } else if (file
.isDown()) {
221 if (PreviewMediaFragment
.canBePreviewed(file
)) {
223 ((FileDisplayActivity
)mContainerActivity
).startMediaPreview(file
, 0, true
);
225 mContainerActivity
.getFileOperationsHelper().openFile(file
);
229 // automatic download, preview on finish
230 ((FileDisplayActivity
)mContainerActivity
).startDownloadForPreview(file
);
236 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
245 public void onCreateContextMenu (
246 ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
247 super.onCreateContextMenu(menu
, v
, menuInfo
);
248 Bundle args
= getArguments();
249 boolean allowContextualActions
=
250 (args
== null
) ? true
: args
.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS
, true
);
251 if (allowContextualActions
) {
252 MenuInflater inflater
= getActivity().getMenuInflater();
253 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
254 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
255 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
257 if (mContainerActivity
.getStorageManager() != null
) {
258 FileMenuFilter mf
= new FileMenuFilter(
260 mContainerActivity
.getStorageManager().getAccount(),
267 /// TODO break this direct dependency on FileDisplayActivity... if possible
268 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
269 FileFragment frag
= ((FileDisplayActivity
)getActivity()).getSecondFragment();
270 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
271 frag
.getFile().getFileId() == targetFile
.getFileId()) {
272 item
= menu
.findItem(R
.id
.action_see_details
);
274 item
.setVisible(false
);
275 item
.setEnabled(false
);
286 public boolean onContextItemSelected (MenuItem item
) {
287 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
288 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
289 switch (item
.getItemId()) {
290 case R
.id
.action_share_file
: {
291 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
294 case R
.id
.action_open_file_with
: {
295 mContainerActivity
.getFileOperationsHelper().openFile(mTargetFile
);
298 case R
.id
.action_unshare_file
: {
299 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
302 case R
.id
.action_rename_file
: {
303 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(mTargetFile
);
304 dialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_RENAME_FILE
);
307 case R
.id
.action_remove_file
: {
308 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(mTargetFile
);
309 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
312 case R
.id
.action_download_file
:
313 case R
.id
.action_sync_file
: {
314 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
317 case R
.id
.action_cancel_download
:
318 case R
.id
.action_cancel_upload
: {
319 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(mTargetFile
);
322 case R
.id
.action_see_details
: {
323 mContainerActivity
.showDetails(mTargetFile
);
326 case R
.id
.action_send_file
: {
328 if (!mTargetFile
.isDown()) { // Download the file
329 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
330 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(mTargetFile
);
333 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(mTargetFile
);
337 case R
.id
.action_move
: {
338 Intent action
= new Intent(getActivity(), FolderPickerActivity
.class);
340 // Pass mTargetFile that contains info of selected file/folder
341 action
.putExtra(FolderPickerActivity
.EXTRA_FILE
, mTargetFile
);
342 getActivity().startActivityForResult(action
, FileDisplayActivity
.ACTION_MOVE_FILES
);
345 case R
.id
.action_favorite_file
:{
346 mContainerActivity
.getFileOperationsHelper().toggleKeepInSync(mTargetFile
, true
);
349 case R
.id
.action_unfavorite_file
:{
350 mContainerActivity
.getFileOperationsHelper().toggleKeepInSync(mTargetFile
, false
);
354 return super.onContextItemSelected(item
);
360 * Use this to query the {@link OCFile} that is currently
361 * being displayed by this fragment
362 * @return The currently viewed OCFile
364 public OCFile
getCurrentFile(){
369 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
371 public void listDirectory(/*boolean onlyOnDevice*/){
373 // TODO Enable when "On Device" is recovered ?
374 // listDirectory(null, onlyOnDevice);
377 public void refreshDirectory(){
378 // TODO Enable when "On Device" is recovered ?
379 listDirectory(getCurrentFile()/*, MainApp.getOnlyOnDevice()*/);
383 * Lists the given directory on the view. When the input parameter is null,
384 * it will either refresh the last known directory. list the root
385 * if there never was a directory.
387 * @param directory File to be listed
389 public void listDirectory(OCFile directory
/*, boolean onlyOnDevice*/) {
390 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
391 if (storageManager
!= null
) {
393 // Check input parameters for null
394 if(directory
== null
){
398 directory
= storageManager
.getFileByPath("/");
399 if (directory
== null
) return; // no files, wait for sync
404 // If that's not a directory -> List its parent
405 if(!directory
.isFolder()){
406 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
407 directory
= storageManager
.getFileById(directory
.getParentId());
410 // TODO Enable when "On Device" is recovered ?
411 mAdapter
.swapDirectory(directory
, storageManager
/*, onlyOnDevice*/);
412 if (mFile
== null
|| !mFile
.equals(directory
)) {
413 mCurrentListView
.setSelection(0);
422 private void updateLayout() {
424 int filesCount
= 0, foldersCount
= 0, imagesCount
= 0;
425 int count
= mAdapter
.getCount();
427 for (int i
=0; i
< count
; i
++) {
428 file
= (OCFile
) mAdapter
.getItem(i
);
429 if (file
.isFolder()) {
439 setFooterText(generateFooterText(filesCount
, foldersCount
));
441 // decide grid vs list view
442 OwnCloudVersion version
= AccountUtils
.getServerVersion(
443 ((FileActivity
)mContainerActivity
).getAccount());
444 if (version
!= null
&& version
.supportsRemoteThumbnails() &&
445 imagesCount
> 0 && imagesCount
== filesCount
) {
453 private String
generateFooterText(int filesCount
, int foldersCount
) {
455 if (filesCount
<= 0) {
456 if (foldersCount
<= 0) {
459 } else if (foldersCount
== 1) {
460 output
= getResources().getString(R
.string
.file_list__footer__folder
);
462 } else { // foldersCount > 1
463 output
= getResources().getString(R
.string
.file_list__footer__folders
, foldersCount
);
466 } else if (filesCount
== 1) {
467 if (foldersCount
<= 0) {
468 output
= getResources().getString(R
.string
.file_list__footer__file
);
470 } else if (foldersCount
== 1) {
471 output
= getResources().getString(R
.string
.file_list__footer__file_and_folder
);
473 } else { // foldersCount > 1
474 output
= getResources().getString(R
.string
.file_list__footer__file_and_folders
, foldersCount
);
476 } else { // filesCount > 1
477 if (foldersCount
<= 0) {
478 output
= getResources().getString(R
.string
.file_list__footer__files
, filesCount
);
480 } else if (foldersCount
== 1) {
481 output
= getResources().getString(R
.string
.file_list__footer__files_and_folder
, filesCount
);
483 } else { // foldersCount > 1
484 output
= getResources().getString(
485 R
.string
.file_list__footer__files_and_folders
, filesCount
, foldersCount
494 public void sortByName(boolean descending
) {
495 mAdapter
.setSortOrder(FileStorageUtils
.SORT_NAME
, descending
);
498 public void sortByDate(boolean descending
) {
499 mAdapter
.setSortOrder(FileStorageUtils
.SORT_DATE
, descending
);
502 public void sortBySize(boolean descending
) {
503 mAdapter
.setSortOrder(FileStorageUtils
.SORT_SIZE
, descending
);