1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2014 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.fragment
;
21 import java
.util
.ArrayList
;
22 import java
.util
.List
;
24 import com
.owncloud
.android
.R
;
25 import com
.owncloud
.android
.authentication
.AccountUtils
;
26 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
27 import com
.owncloud
.android
.datamodel
.OCFile
;
28 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
29 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
30 import com
.owncloud
.android
.lib
.common
.operations
.OnRemoteOperationListener
;
31 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
32 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
33 import com
.owncloud
.android
.operations
.RenameFileOperation
;
34 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
35 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
36 import com
.owncloud
.android
.ui
.activity
.TransferServiceGetter
;
37 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
38 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
39 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
40 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
;
41 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
42 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
43 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
44 import com
.owncloud
.android
.utils
.Log_OC
;
46 import android
.accounts
.Account
;
47 import android
.app
.Activity
;
48 import android
.database
.Cursor
;
49 import android
.os
.Bundle
;
50 import android
.os
.Handler
;
51 import android
.view
.ContextMenu
;
52 import android
.view
.MenuInflater
;
53 import android
.view
.MenuItem
;
54 import android
.view
.View
;
55 import android
.widget
.AdapterView
;
56 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
59 * A Fragment that lists all files and folders in a given path.
61 * @author Bartek Przybylski
64 public class OCFileListFragment
extends ExtendedListFragment
implements EditNameDialogListener
, ConfirmationDialogFragmentListener
{
66 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
68 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ? OCFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
69 private static final String EXTRA_FILE
= MY_PACKAGE
+ ".extra.FILE";
71 private static final String KEY_INDEXES
= "INDEXES";
72 private static final String KEY_FIRST_POSITIONS
= "FIRST_POSITIONS";
73 private static final String KEY_TOPS
= "TOPS";
74 private static final String KEY_HEIGHT_CELL
= "HEIGHT_CELL";
76 private OCFileListFragment
.ContainerActivity mContainerActivity
;
78 private OCFile mFile
= null
;
79 private FileListListAdapter mAdapter
;
81 private Handler mHandler
;
82 private OCFile mTargetFile
;
84 // Save the state of the scroll in browsing
85 private ArrayList
<Integer
> mIndexes
;
86 private ArrayList
<Integer
> mFirstPositions
;
87 private ArrayList
<Integer
> mTops
;
89 private int mHeightCell
= 0;
95 public void onAttach(Activity activity
) {
96 super.onAttach(activity
);
97 Log_OC
.e(TAG
, "onAttach");
99 mContainerActivity
= (ContainerActivity
) activity
;
100 } catch (ClassCastException e
) {
101 throw new ClassCastException(activity
.toString() + " must implement " + OCFileListFragment
.ContainerActivity
.class.getSimpleName());
110 public void onActivityCreated(Bundle savedInstanceState
) {
111 super.onActivityCreated(savedInstanceState
);
112 Log_OC
.e(TAG
, "onActivityCreated() start");
114 mAdapter
= new FileListListAdapter(getSherlockActivity(), mContainerActivity
);
116 if (savedInstanceState
!= null
) {
117 mFile
= savedInstanceState
.getParcelable(EXTRA_FILE
);
118 mIndexes
= savedInstanceState
.getIntegerArrayList(KEY_INDEXES
);
119 mFirstPositions
= savedInstanceState
.getIntegerArrayList(KEY_FIRST_POSITIONS
);
120 mTops
= savedInstanceState
.getIntegerArrayList(KEY_TOPS
);
121 mHeightCell
= savedInstanceState
.getInt(KEY_HEIGHT_CELL
);
124 mIndexes
= new ArrayList
<Integer
>();
125 mFirstPositions
= new ArrayList
<Integer
>();
126 mTops
= new ArrayList
<Integer
>();
131 setListAdapter(mAdapter
);
133 registerForContextMenu(getListView());
134 getListView().setOnCreateContextMenuListener(this);
136 mHandler
= new Handler();
141 * Saves the current listed folder.
144 public void onSaveInstanceState (Bundle outState
) {
145 super.onSaveInstanceState(outState
);
146 outState
.putParcelable(EXTRA_FILE
, mFile
);
147 outState
.putIntegerArrayList(KEY_INDEXES
, mIndexes
);
148 outState
.putIntegerArrayList(KEY_FIRST_POSITIONS
, mFirstPositions
);
149 outState
.putIntegerArrayList(KEY_TOPS
, mTops
);
150 outState
.putInt(KEY_HEIGHT_CELL
, mHeightCell
);
154 * Call this, when the user presses the up button.
156 * Tries to move up the current folder one level. If the parent folder was removed from the database,
157 * it continues browsing up until finding an existing folders.
159 * return Count of folder levels browsed up.
161 public int onBrowseUp() {
162 OCFile parentDir
= null
;
166 FileDataStorageManager storageManager
=
167 ((FileActivity
)getSherlockActivity()).getStorageManager();
169 String parentPath
= null
;
170 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
171 parentPath
= new File(mFile
.getRemotePath()).getParent();
172 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
173 parentDir
= storageManager
.getFileByPath(parentPath
);
176 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
); // never returns null; keep the path in root folder
178 while (parentDir
== null
) {
179 parentPath
= new File(parentPath
).getParent();
180 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
181 parentDir
= storageManager
.getFileByPath(parentPath
);
183 } // exit is granted because storageManager.getFileByPath("/") never returns null
188 listDirectory(mFile
);
190 mContainerActivity
.startSyncFolderOperation(mFile
);
192 // restore index and top position
193 restoreIndexAndTopPosition();
195 } // else - should never happen now
201 * Restore index and position
203 private void restoreIndexAndTopPosition() {
204 if (mIndexes
.size() > 0) {
205 // needs to be checked; not every browse-up had a browse-down before
207 int index
= mIndexes
.remove(mIndexes
.size() - 1);
209 int firstPosition
= mFirstPositions
.remove(mFirstPositions
.size() -1);
211 int top
= mTops
.remove(mTops
.size() - 1);
213 mList
.setSelectionFromTop(firstPosition
, top
);
215 // Move the scroll if the selection is not visible
216 int indexPosition
= mHeightCell
*index
;
217 int height
= mList
.getHeight();
219 if (indexPosition
> height
) {
220 if (android
.os
.Build
.VERSION
.SDK_INT
>= 11)
222 mList
.smoothScrollToPosition(index
);
224 else if (android
.os
.Build
.VERSION
.SDK_INT
>= 8)
226 mList
.setSelectionFromTop(index
, 0);
234 * Save index and top position
236 private void saveIndexAndTopPosition(int index
) {
240 int firstPosition
= mList
.getFirstVisiblePosition();
241 mFirstPositions
.add(firstPosition
);
243 View view
= mList
.getChildAt(0);
244 int top
= (view
== null
) ?
0 : view
.getTop() ;
248 // Save the height of a cell
249 mHeightCell
= (view
== null
|| mHeightCell
!= 0) ? mHeightCell
: view
.getHeight();
253 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
254 OCFile file
= ((FileActivity
)getSherlockActivity()).getStorageManager().createFileInstance(
255 (Cursor
) mAdapter
.getItem(position
));
257 if (file
.isFolder()) {
258 // update state and view of this fragment
260 // then, notify parent activity to let it update its state and view, and other fragments
261 mContainerActivity
.onBrowsedDownTo(file
);
262 // save index and top position
263 saveIndexAndTopPosition(position
);
265 } else { /// Click on a file
266 if (PreviewImageFragment
.canBePreviewed(file
)) {
267 // preview image - it handles the download, if needed
268 mContainerActivity
.startImagePreview(file
);
270 } else if (file
.isDown()) {
271 if (PreviewMediaFragment
.canBePreviewed(file
)) {
273 mContainerActivity
.startMediaPreview(file
, 0, true
);
275 FileActivity activity
= (FileActivity
) getSherlockActivity();
276 activity
.getFileOperationsHelper().openFile(file
, activity
);
280 // automatic download, preview on finish
281 mContainerActivity
.startDownloadForPreview(file
);
287 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
296 public void onCreateContextMenu (ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
297 super.onCreateContextMenu(menu
, v
, menuInfo
);
298 MenuInflater inflater
= getSherlockActivity().getMenuInflater();
299 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
300 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
301 OCFile targetFile
= ((FileActivity
)getSherlockActivity()).getStorageManager().createFileInstance(
302 (Cursor
) mAdapter
.getItem(info
.position
));
303 List
<Integer
> toHide
= new ArrayList
<Integer
>();
304 List
<Integer
> toDisable
= new ArrayList
<Integer
>();
306 MenuItem item
= null
;
307 if (targetFile
.isFolder()) {
308 // contextual menu for folders
309 toHide
.add(R
.id
.action_open_file_with
);
310 toHide
.add(R
.id
.action_download_file
);
311 toHide
.add(R
.id
.action_cancel_download
);
312 toHide
.add(R
.id
.action_cancel_upload
);
313 toHide
.add(R
.id
.action_sync_file
);
314 toHide
.add(R
.id
.action_see_details
);
315 toHide
.add(R
.id
.action_send_file
);
316 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile
) ||
317 mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile
) ) {
318 toDisable
.add(R
.id
.action_rename_file
);
319 toDisable
.add(R
.id
.action_remove_file
);
324 // contextual menu for regular files
326 // new design: 'download' and 'open with' won't be available anymore in context menu
327 toHide
.add(R
.id
.action_download_file
);
328 toHide
.add(R
.id
.action_open_file_with
);
330 if (targetFile
.isDown()) {
331 toHide
.add(R
.id
.action_cancel_download
);
332 toHide
.add(R
.id
.action_cancel_upload
);
335 toHide
.add(R
.id
.action_sync_file
);
337 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile
)) {
338 toHide
.add(R
.id
.action_cancel_upload
);
339 toDisable
.add(R
.id
.action_rename_file
);
340 toDisable
.add(R
.id
.action_remove_file
);
342 } else if ( mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile
)) {
343 toHide
.add(R
.id
.action_cancel_download
);
344 toDisable
.add(R
.id
.action_rename_file
);
345 toDisable
.add(R
.id
.action_remove_file
);
348 toHide
.add(R
.id
.action_cancel_download
);
349 toHide
.add(R
.id
.action_cancel_upload
);
354 if (!targetFile
.isShareByLink()) {
355 toHide
.add(R
.id
.action_unshare_file
);
359 boolean sendEnabled
= getString(R
.string
.send_files_to_other_apps
).equalsIgnoreCase("on");
361 toHide
.add(R
.id
.action_send_file
);
364 for (int i
: toHide
) {
365 item
= menu
.findItem(i
);
367 item
.setVisible(false
);
368 item
.setEnabled(false
);
372 for (int i
: toDisable
) {
373 item
= menu
.findItem(i
);
375 item
.setEnabled(false
);
385 public boolean onContextItemSelected (MenuItem item
) {
386 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
387 mTargetFile
= ((FileActivity
)getSherlockActivity()).getStorageManager().createFileInstance(
388 (Cursor
) mAdapter
.getItem(info
.position
));
389 switch (item
.getItemId()) {
390 case R
.id
.action_share_file
: {
391 FileActivity activity
= (FileActivity
) getSherlockActivity();
392 activity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
, activity
);
395 case R
.id
.action_unshare_file
: {
396 FileActivity activity
= (FileActivity
) getSherlockActivity();
397 activity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
, activity
);
400 case R
.id
.action_rename_file
: {
401 String fileName
= mTargetFile
.getFileName();
402 int extensionStart
= mTargetFile
.isFolder() ?
-1 : fileName
.lastIndexOf(".");
403 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: fileName
.length();
404 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), fileName
, 0, selectionEnd
, this);
405 dialog
.show(getFragmentManager(), EditNameDialog
.TAG
);
408 case R
.id
.action_remove_file
: {
409 int messageStringId
= R
.string
.confirmation_remove_alert
;
410 int posBtnStringId
= R
.string
.confirmation_remove_remote
;
411 int neuBtnStringId
= -1;
412 if (mTargetFile
.isFolder()) {
413 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
414 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
415 neuBtnStringId
= R
.string
.confirmation_remove_folder_local
;
416 } else if (mTargetFile
.isDown()) {
417 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
418 neuBtnStringId
= R
.string
.confirmation_remove_local
;
420 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
422 new String
[]{mTargetFile
.getFileName()},
425 R
.string
.common_cancel
);
426 confDialog
.setOnConfirmationListener(this);
427 confDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
430 case R
.id
.action_sync_file
: {
431 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
432 RemoteOperation operation
= new SynchronizeFileOperation(
435 ((FileActivity
)getSherlockActivity()).getStorageManager(),
438 getSherlockActivity());
439 operation
.execute(account
, getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
440 ((FileActivity
) getSherlockActivity()).showLoadingDialog();
443 case R
.id
.action_cancel_download
: {
444 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
445 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
446 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, mTargetFile
)) {
447 downloaderBinder
.cancel(account
, mTargetFile
);
449 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
453 case R
.id
.action_cancel_upload
: {
454 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
455 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
456 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, mTargetFile
)) {
457 uploaderBinder
.cancel(account
, mTargetFile
);
459 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
463 case R
.id
.action_see_details
: {
464 ((FileFragment
.ContainerActivity
)getSherlockActivity()).showDetails(mTargetFile
);
467 case R
.id
.action_send_file
: {
469 if (!mTargetFile
.isDown()) { // Download the file
470 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
471 mContainerActivity
.startDownloadForSending(mTargetFile
);
475 FileActivity activity
= (FileActivity
) getSherlockActivity();
476 activity
.getFileOperationsHelper().sendDownloadedFile(mTargetFile
, activity
);
481 return super.onContextItemSelected(item
);
487 * Use this to query the {@link OCFile} that is currently
488 * being displayed by this fragment
489 * @return The currently viewed OCFile
491 public OCFile
getCurrentFile(){
496 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
498 public void listDirectory(){
503 * Lists the given directory on the view. When the input parameter is null,
504 * it will either refresh the last known directory. list the root
505 * if there never was a directory.
507 * @param directory File to be listed
509 public void listDirectory(OCFile directory
) {
510 FileDataStorageManager storageManager
= ((FileActivity
)getSherlockActivity()).getStorageManager();
511 if (storageManager
!= null
) {
513 // Check input parameters for null
514 if(directory
== null
){
518 directory
= storageManager
.getFileByPath("/");
519 if (directory
== null
) return; // no files, wait for sync
524 // If that's not a directory -> List its parent
525 if(!directory
.isFolder()){
526 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
527 directory
= storageManager
.getFileById(directory
.getParentId());
530 mAdapter
.swapDirectory(directory
, storageManager
);
531 if (mFile
== null
|| !mFile
.equals(directory
)) {
532 mList
.setSelectionFromTop(0, 0);
541 * Interface to implement by any Activity that includes some instance of FileListFragment
543 * @author David A. Velasco
545 public interface ContainerActivity
extends TransferServiceGetter
, OnRemoteOperationListener
{
548 * Callback method invoked when a the user browsed into a different folder through the list of files
552 public void onBrowsedDownTo(OCFile folder
);
554 public void startDownloadForPreview(OCFile file
);
556 public void startMediaPreview(OCFile file
, int i
, boolean b
);
558 public void startImagePreview(OCFile file
);
560 public void startSyncFolderOperation(OCFile folder
);
563 * Callback method invoked when a the 'transfer state' of a file changes.
565 * This happens when a download or upload is started or ended for a file.
567 * This method is necessary by now to update the user interface of the double-pane layout in tablets
568 * because methods {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and {@link FileUploaderBinder#isUploading(Account, OCFile)}
569 * won't provide the needed response before the method where this is called finishes.
571 * TODO Remove this when the transfer state of a file is kept in the database (other thing TODO)
573 * @param file OCFile which state changed.
574 * @param downloading Flag signaling if the file is now downloading.
575 * @param uploading Flag signaling if the file is now uploading.
577 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
);
579 public void startDownloadForSending(OCFile file
);
585 public void onDismiss(EditNameDialog dialog
) {
586 if (dialog
.getResult()) {
587 String newFilename
= dialog
.getNewFilename();
588 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
589 RemoteOperation operation
=
590 new RenameFileOperation(
592 AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()),
594 ((FileActivity
)getSherlockActivity()).getStorageManager());
595 operation
.execute(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
596 ((FileActivity
) getSherlockActivity()).showLoadingDialog();
602 public void onConfirmation(String callerTag
) {
603 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
604 FileDataStorageManager storageManager
=
605 ((FileActivity
)getSherlockActivity()).getStorageManager();
606 if (storageManager
.getFileById(mTargetFile
.getFileId()) != null
) {
607 RemoteOperation operation
= new RemoveFileOperation( mTargetFile
,
610 operation
.execute(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
612 ((FileActivity
) getSherlockActivity()).showLoadingDialog();
618 public void onNeutral(String callerTag
) {
619 ((FileActivity
)getSherlockActivity()).getStorageManager().removeFile(mTargetFile
, false
, true
); // TODO perform in background task / new thread
621 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
625 public void onCancel(String callerTag
) {
626 Log_OC
.d(TAG
, "REMOVAL CANCELED");