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
.FileDisplayActivity
;
36 import com
.owncloud
.android
.ui
.activity
.TransferServiceGetter
;
37 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
38 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
39 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
40 import com
.owncloud
.android
.ui
.fragment
.ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
;
41 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
42 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
43 import com
.owncloud
.android
.utils
.Log_OC
;
45 import android
.accounts
.Account
;
46 import android
.app
.Activity
;
47 import android
.os
.Bundle
;
48 import android
.os
.Handler
;
49 import android
.view
.ContextMenu
;
50 import android
.view
.MenuInflater
;
51 import android
.view
.MenuItem
;
52 import android
.view
.View
;
53 import android
.widget
.AdapterView
;
54 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
57 * A Fragment that lists all files and folders in a given path.
59 * @author Bartek Przybylski
62 public class OCFileListFragment
extends ExtendedListFragment
implements EditNameDialogListener
, ConfirmationDialogFragmentListener
{
64 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
66 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ? OCFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
67 private static final String EXTRA_FILE
= MY_PACKAGE
+ ".extra.FILE";
69 private static final String KEY_INDEXES
= "INDEXES";
70 private static final String KEY_FIRST_POSITIONS
= "FIRST_POSITIONS";
71 private static final String KEY_TOPS
= "TOPS";
72 private static final String KEY_HEIGHT_CELL
= "HEIGHT_CELL";
74 private OCFileListFragment
.ContainerActivity mContainerActivity
;
76 private OCFile mFile
= null
;
77 private FileListListAdapter mAdapter
;
79 private Handler mHandler
;
80 private OCFile mTargetFile
;
82 // Save the state of the scroll in browsing
83 private ArrayList
<Integer
> mIndexes
;
84 private ArrayList
<Integer
> mFirstPositions
;
85 private ArrayList
<Integer
> mTops
;
87 private int mHeightCell
= 0;
93 public void onAttach(Activity activity
) {
94 super.onAttach(activity
);
95 Log_OC
.e(TAG
, "onAttach");
97 mContainerActivity
= (ContainerActivity
) activity
;
98 } catch (ClassCastException e
) {
99 throw new ClassCastException(activity
.toString() + " must implement " + OCFileListFragment
.ContainerActivity
.class.getSimpleName());
108 public void onActivityCreated(Bundle savedInstanceState
) {
109 super.onActivityCreated(savedInstanceState
);
110 Log_OC
.e(TAG
, "onActivityCreated() start");
111 mAdapter
= new FileListListAdapter(getActivity(), mContainerActivity
);
112 if (savedInstanceState
!= null
) {
113 mFile
= savedInstanceState
.getParcelable(EXTRA_FILE
);
114 mIndexes
= savedInstanceState
.getIntegerArrayList(KEY_INDEXES
);
115 mFirstPositions
= savedInstanceState
.getIntegerArrayList(KEY_FIRST_POSITIONS
);
116 mTops
= savedInstanceState
.getIntegerArrayList(KEY_TOPS
);
117 mHeightCell
= savedInstanceState
.getInt(KEY_HEIGHT_CELL
);
120 mIndexes
= new ArrayList
<Integer
>();
121 mFirstPositions
= new ArrayList
<Integer
>();
122 mTops
= new ArrayList
<Integer
>();
127 setListAdapter(mAdapter
);
129 registerForContextMenu(getListView());
130 getListView().setOnCreateContextMenuListener(this);
132 mHandler
= new Handler();
137 * Saves the current listed folder.
140 public void onSaveInstanceState (Bundle outState
) {
141 super.onSaveInstanceState(outState
);
142 outState
.putParcelable(EXTRA_FILE
, mFile
);
143 outState
.putIntegerArrayList(KEY_INDEXES
, mIndexes
);
144 outState
.putIntegerArrayList(KEY_FIRST_POSITIONS
, mFirstPositions
);
145 outState
.putIntegerArrayList(KEY_TOPS
, mTops
);
146 outState
.putInt(KEY_HEIGHT_CELL
, mHeightCell
);
150 * Call this, when the user presses the up button.
152 * Tries to move up the current folder one level. If the parent folder was removed from the database,
153 * it continues browsing up until finding an existing folders.
155 * return Count of folder levels browsed up.
157 public int onBrowseUp() {
158 OCFile parentDir
= null
;
162 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
164 String parentPath
= null
;
165 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
166 parentPath
= new File(mFile
.getRemotePath()).getParent();
167 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
168 parentDir
= storageManager
.getFileByPath(parentPath
);
171 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
); // never returns null; keep the path in root folder
173 while (parentDir
== null
) {
174 parentPath
= new File(parentPath
).getParent();
175 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
176 parentDir
= storageManager
.getFileByPath(parentPath
);
178 } // exit is granted because storageManager.getFileByPath("/") never returns null
183 listDirectory(mFile
);
185 mContainerActivity
.startSyncFolderOperation(mFile
);
187 // restore index and top position
188 restoreIndexAndTopPosition();
190 } // else - should never happen now
196 * Restore index and position
198 private void restoreIndexAndTopPosition() {
199 if (mIndexes
.size() > 0) {
200 // needs to be checked; not every browse-up had a browse-down before
202 int index
= mIndexes
.remove(mIndexes
.size() - 1);
204 int firstPosition
= mFirstPositions
.remove(mFirstPositions
.size() -1);
206 int top
= mTops
.remove(mTops
.size() - 1);
208 mList
.setSelectionFromTop(firstPosition
, top
);
210 // Move the scroll if the selection is not visible
211 int indexPosition
= mHeightCell
*index
;
212 int height
= mList
.getHeight();
214 if (indexPosition
> height
) {
215 if (android
.os
.Build
.VERSION
.SDK_INT
>= 11)
217 mList
.smoothScrollToPosition(index
);
219 else if (android
.os
.Build
.VERSION
.SDK_INT
>= 8)
221 mList
.setSelectionFromTop(index
, 0);
229 * Save index and top position
231 private void saveIndexAndTopPosition(int index
) {
235 int firstPosition
= mList
.getFirstVisiblePosition();
236 mFirstPositions
.add(firstPosition
);
238 View view
= mList
.getChildAt(0);
239 int top
= (view
== null
) ?
0 : view
.getTop() ;
243 // Save the height of a cell
244 mHeightCell
= (view
== null
|| mHeightCell
!= 0) ? mHeightCell
: view
.getHeight();
248 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
249 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
251 if (file
.isFolder()) {
252 // update state and view of this fragment
254 // then, notify parent activity to let it update its state and view, and other fragments
255 mContainerActivity
.onBrowsedDownTo(file
);
256 // save index and top position
257 saveIndexAndTopPosition(position
);
259 } else { /// Click on a file
260 if (PreviewImageFragment
.canBePreviewed(file
)) {
261 // preview image - it handles the download, if needed
262 mContainerActivity
.startImagePreview(file
);
264 } else if (file
.isDown()) {
265 if (PreviewMediaFragment
.canBePreviewed(file
)) {
267 mContainerActivity
.startMediaPreview(file
, 0, true
);
269 FileDisplayActivity activity
= (FileDisplayActivity
) getSherlockActivity();
270 activity
.getFileOperationsHelper().openFile(file
, activity
);
274 // automatic download, preview on finish
275 mContainerActivity
.startDownloadForPreview(file
);
281 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
290 public void onCreateContextMenu (ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
291 super.onCreateContextMenu(menu
, v
, menuInfo
);
292 MenuInflater inflater
= getActivity().getMenuInflater();
293 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
294 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
295 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
296 List
<Integer
> toHide
= new ArrayList
<Integer
>();
297 List
<Integer
> toDisable
= new ArrayList
<Integer
>();
299 MenuItem item
= null
;
300 if (targetFile
.isFolder()) {
301 // contextual menu for folders
302 toHide
.add(R
.id
.action_open_file_with
);
303 toHide
.add(R
.id
.action_download_file
);
304 toHide
.add(R
.id
.action_cancel_download
);
305 toHide
.add(R
.id
.action_cancel_upload
);
306 toHide
.add(R
.id
.action_sync_file
);
307 toHide
.add(R
.id
.action_see_details
);
308 toHide
.add(R
.id
.action_send_file
);
309 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
) ||
310 mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
) ) {
311 toDisable
.add(R
.id
.action_rename_file
);
312 toDisable
.add(R
.id
.action_remove_file
);
317 // contextual menu for regular files
319 // new design: 'download' and 'open with' won't be available anymore in context menu
320 toHide
.add(R
.id
.action_download_file
);
321 toHide
.add(R
.id
.action_open_file_with
);
323 if (targetFile
.isDown()) {
324 toHide
.add(R
.id
.action_cancel_download
);
325 toHide
.add(R
.id
.action_cancel_upload
);
328 toHide
.add(R
.id
.action_sync_file
);
330 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
331 toHide
.add(R
.id
.action_cancel_upload
);
332 toDisable
.add(R
.id
.action_rename_file
);
333 toDisable
.add(R
.id
.action_remove_file
);
335 } else if ( mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
336 toHide
.add(R
.id
.action_cancel_download
);
337 toDisable
.add(R
.id
.action_rename_file
);
338 toDisable
.add(R
.id
.action_remove_file
);
341 toHide
.add(R
.id
.action_cancel_download
);
342 toHide
.add(R
.id
.action_cancel_upload
);
347 if (!targetFile
.isShareByLink()) {
348 toHide
.add(R
.id
.action_unshare_file
);
352 boolean sendEnabled
= getString(R
.string
.send_files_to_other_apps
).equalsIgnoreCase("on");
354 toHide
.add(R
.id
.action_send_file
);
357 for (int i
: toHide
) {
358 item
= menu
.findItem(i
);
360 item
.setVisible(false
);
361 item
.setEnabled(false
);
365 for (int i
: toDisable
) {
366 item
= menu
.findItem(i
);
368 item
.setEnabled(false
);
378 public boolean onContextItemSelected (MenuItem item
) {
379 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
380 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
381 switch (item
.getItemId()) {
382 case R
.id
.action_share_file
: {
383 FileDisplayActivity activity
= (FileDisplayActivity
) getSherlockActivity();
384 activity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
, activity
);
387 case R
.id
.action_unshare_file
: {
388 FileDisplayActivity activity
= (FileDisplayActivity
) getSherlockActivity();
389 activity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
, activity
);
392 case R
.id
.action_rename_file
: {
393 String fileName
= mTargetFile
.getFileName();
394 int extensionStart
= mTargetFile
.isFolder() ?
-1 : fileName
.lastIndexOf(".");
395 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: fileName
.length();
396 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), fileName
, 0, selectionEnd
, this);
397 dialog
.show(getFragmentManager(), EditNameDialog
.TAG
);
400 case R
.id
.action_remove_file
: {
401 int messageStringId
= R
.string
.confirmation_remove_alert
;
402 int posBtnStringId
= R
.string
.confirmation_remove_remote
;
403 int neuBtnStringId
= -1;
404 if (mTargetFile
.isFolder()) {
405 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
406 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
407 neuBtnStringId
= R
.string
.confirmation_remove_folder_local
;
408 } else if (mTargetFile
.isDown()) {
409 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
410 neuBtnStringId
= R
.string
.confirmation_remove_local
;
412 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
414 new String
[]{mTargetFile
.getFileName()},
417 R
.string
.common_cancel
);
418 confDialog
.setOnConfirmationListener(this);
419 confDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
422 case R
.id
.action_sync_file
: {
423 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
424 RemoteOperation operation
= new SynchronizeFileOperation(mTargetFile
, null
, mContainerActivity
.getStorageManager(), account
, true
, getSherlockActivity());
425 operation
.execute(account
, getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
426 ((FileDisplayActivity
) getSherlockActivity()).showLoadingDialog();
429 case R
.id
.action_cancel_download
: {
430 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
431 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
432 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, mTargetFile
)) {
433 downloaderBinder
.cancel(account
, mTargetFile
);
435 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
439 case R
.id
.action_cancel_upload
: {
440 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
441 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
442 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, mTargetFile
)) {
443 uploaderBinder
.cancel(account
, mTargetFile
);
445 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
449 case R
.id
.action_see_details
: {
450 ((FileFragment
.ContainerActivity
)getActivity()).showDetails(mTargetFile
);
453 case R
.id
.action_send_file
: {
455 if (!mTargetFile
.isDown()) { // Download the file
456 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
457 mContainerActivity
.startDownloadForSending(mTargetFile
);
461 FileDisplayActivity activity
= (FileDisplayActivity
) getSherlockActivity();
462 activity
.getFileOperationsHelper().sendDownloadedFile(mTargetFile
, activity
);
467 return super.onContextItemSelected(item
);
473 * Use this to query the {@link OCFile} that is currently
474 * being displayed by this fragment
475 * @return The currently viewed OCFile
477 public OCFile
getCurrentFile(){
482 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
484 public void listDirectory(){
489 * Lists the given directory on the view. When the input parameter is null,
490 * it will either refresh the last known directory. list the root
491 * if there never was a directory.
493 * @param directory File to be listed
495 public void listDirectory(OCFile directory
) {
496 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
497 if (storageManager
!= null
) {
499 // Check input parameters for null
500 if(directory
== null
){
504 directory
= storageManager
.getFileByPath("/");
505 if (directory
== null
) return; // no files, wait for sync
510 // If that's not a directory -> List its parent
511 if(!directory
.isFolder()){
512 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
513 directory
= storageManager
.getFileById(directory
.getParentId());
516 mAdapter
.swapDirectory(directory
, storageManager
);
517 if (mFile
== null
|| !mFile
.equals(directory
)) {
518 mList
.setSelectionFromTop(0, 0);
527 * Interface to implement by any Activity that includes some instance of FileListFragment
529 * @author David A. Velasco
531 public interface ContainerActivity
extends TransferServiceGetter
, OnRemoteOperationListener
{
534 * Callback method invoked when a the user browsed into a different folder through the list of files
538 public void onBrowsedDownTo(OCFile folder
);
540 public void startDownloadForPreview(OCFile file
);
542 public void startMediaPreview(OCFile file
, int i
, boolean b
);
544 public void startImagePreview(OCFile file
);
546 public void startSyncFolderOperation(OCFile folder
);
549 * Getter for the current DataStorageManager in the container activity
551 public FileDataStorageManager
getStorageManager();
555 * Callback method invoked when a the 'transfer state' of a file changes.
557 * This happens when a download or upload is started or ended for a file.
559 * This method is necessary by now to update the user interface of the double-pane layout in tablets
560 * because methods {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and {@link FileUploaderBinder#isUploading(Account, OCFile)}
561 * won't provide the needed response before the method where this is called finishes.
563 * TODO Remove this when the transfer state of a file is kept in the database (other thing TODO)
565 * @param file OCFile which state changed.
566 * @param downloading Flag signaling if the file is now downloading.
567 * @param uploading Flag signaling if the file is now uploading.
569 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
);
571 void startDownloadForSending(OCFile file
);
577 public void onDismiss(EditNameDialog dialog
) {
578 if (dialog
.getResult()) {
579 String newFilename
= dialog
.getNewFilename();
580 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
581 RemoteOperation operation
= new RenameFileOperation(mTargetFile
,
582 AccountUtils
.getCurrentOwnCloudAccount(getActivity()),
584 mContainerActivity
.getStorageManager());
585 operation
.execute(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
586 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
592 public void onConfirmation(String callerTag
) {
593 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
594 if (mContainerActivity
.getStorageManager().getFileById(mTargetFile
.getFileId()) != null
) {
595 RemoteOperation operation
= new RemoveFileOperation( mTargetFile
,
597 mContainerActivity
.getStorageManager());
598 operation
.execute(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
600 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
606 public void onNeutral(String callerTag
) {
607 mContainerActivity
.getStorageManager().removeFile(mTargetFile
, false
, true
); // TODO perform in background task / new thread
609 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
613 public void onCancel(String callerTag
) {
614 Log_OC
.d(TAG
, "REMOVAL CANCELED");