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
.FileListCursorLoader
;
28 import com
.owncloud
.android
.datamodel
.OCFile
;
29 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
30 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
31 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
32 import com
.owncloud
.android
.ui
.ExtendedListView
;
33 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
34 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
35 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
36 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
37 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
;
38 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
39 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
40 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
41 import com
.owncloud
.android
.utils
.Log_OC
;
43 import android
.accounts
.Account
;
44 import android
.app
.Activity
;
45 import android
.database
.Cursor
;
46 import android
.net
.Uri
;
47 import android
.os
.Bundle
;
48 import android
.support
.v4
.app
.LoaderManager
;
49 import android
.support
.v4
.app
.LoaderManager
.LoaderCallbacks
;
50 import android
.support
.v4
.content
.Loader
;
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 * TODO refactorize to get rid of direct dependency on FileDisplayActivity
63 * @author Bartek Przybylski
65 * @author David A. Velasco
67 public class OCFileListFragment
extends ExtendedListFragment
68 implements EditNameDialogListener
, ConfirmationDialogFragmentListener
,
69 LoaderCallbacks
<Cursor
>{
71 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
73 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ? OCFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
74 private static final String EXTRA_FILE
= MY_PACKAGE
+ ".extra.FILE";
76 private static final String KEY_INDEXES
= "INDEXES";
77 private static final String KEY_FIRST_POSITIONS
= "FIRST_POSITIONS";
78 private static final String KEY_TOPS
= "TOPS";
79 private static final String KEY_HEIGHT_CELL
= "HEIGHT_CELL";
81 private static final int LOADER_ID
= 0;
83 private FileFragment
.ContainerActivity mContainerActivity
;
85 private OCFile mFile
= null
;
86 private FileListListAdapter mAdapter
;
87 private LoaderManager mLoaderManager
;
88 private FileListCursorLoader mCursorLoader
;
90 private OCFile mTargetFile
;
92 // Save the state of the scroll in browsing
93 private ArrayList
<Integer
> mIndexes
;
94 private ArrayList
<Integer
> mFirstPositions
;
95 private ArrayList
<Integer
> mTops
;
97 private int mHeightCell
= 0;
103 public void onAttach(Activity activity
) {
104 super.onAttach(activity
);
105 Log_OC
.e(TAG
, "onAttach");
107 mContainerActivity
= (FileFragment
.ContainerActivity
) activity
;
108 } catch (ClassCastException e
) {
109 throw new ClassCastException(activity
.toString() + " must implement " +
110 FileFragment
.ContainerActivity
.class.getSimpleName());
116 public void onDetach() {
117 mContainerActivity
= null
;
125 public void onActivityCreated(Bundle savedInstanceState
) {
126 super.onActivityCreated(savedInstanceState
);
127 Log_OC
.e(TAG
, "onActivityCreated() start");
129 mAdapter
= new FileListListAdapter(getSherlockActivity(), mContainerActivity
);
130 mLoaderManager
= getLoaderManager();
132 if (savedInstanceState
!= null
) {
133 mFile
= savedInstanceState
.getParcelable(EXTRA_FILE
);
134 mIndexes
= savedInstanceState
.getIntegerArrayList(KEY_INDEXES
);
135 mFirstPositions
= savedInstanceState
.getIntegerArrayList(KEY_FIRST_POSITIONS
);
136 mTops
= savedInstanceState
.getIntegerArrayList(KEY_TOPS
);
137 onCreateLoader(LOADER_ID
, null
);
140 mIndexes
= new ArrayList
<Integer
>();
141 mFirstPositions
= new ArrayList
<Integer
>();
142 mTops
= new ArrayList
<Integer
>();
147 // Initialize loaderManager and makes it active
148 mLoaderManager
.initLoader(LOADER_ID
, null
, this);
150 setListAdapter(mAdapter
);
152 registerForContextMenu(getListView());
153 getListView().setOnCreateContextMenuListener(this);
158 * Saves the current listed folder.
161 public void onSaveInstanceState (Bundle outState
) {
162 super.onSaveInstanceState(outState
);
163 outState
.putParcelable(EXTRA_FILE
, mFile
);
164 outState
.putIntegerArrayList(KEY_INDEXES
, mIndexes
);
165 outState
.putIntegerArrayList(KEY_FIRST_POSITIONS
, mFirstPositions
);
166 outState
.putIntegerArrayList(KEY_TOPS
, mTops
);
167 outState
.putInt(KEY_HEIGHT_CELL
, mHeightCell
);
172 * Call this, when the user presses the up button.
174 * Tries to move up the current folder one level. If the parent folder was removed from the database,
175 * it continues browsing up until finding an existing folders.
177 * return Count of folder levels browsed up.
179 public int onBrowseUp() {
180 OCFile parentDir
= null
;
184 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
186 String parentPath
= null
;
187 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
188 parentPath
= new File(mFile
.getRemotePath()).getParent();
189 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
190 parentDir
= storageManager
.getFileByPath(parentPath
);
193 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
); // never returns null; keep the path in root folder
195 while (parentDir
== null
) {
196 parentPath
= new File(parentPath
).getParent();
197 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
198 parentDir
= storageManager
.getFileByPath(parentPath
);
200 } // exit is granted because storageManager.getFileByPath("/") never returns null
205 listDirectory(mFile
);
207 ((FileDisplayActivity
)mContainerActivity
).startSyncFolderOperation(mFile
);
209 // restore index and top position
210 restoreIndexAndTopPosition();
212 } // else - should never happen now
218 * Restore index and position
220 private void restoreIndexAndTopPosition() {
221 if (mIndexes
.size() > 0) {
222 // needs to be checked; not every browse-up had a browse-down before
224 int index
= mIndexes
.remove(mIndexes
.size() - 1);
226 int firstPosition
= mFirstPositions
.remove(mFirstPositions
.size() -1);
228 int top
= mTops
.remove(mTops
.size() - 1);
230 ExtendedListView list
= (ExtendedListView
) getListView();
231 list
.setSelectionFromTop(firstPosition
, top
);
233 // Move the scroll if the selection is not visible
234 int indexPosition
= mHeightCell
*index
;
235 int height
= list
.getHeight();
237 if (indexPosition
> height
) {
238 if (android
.os
.Build
.VERSION
.SDK_INT
>= 11)
240 list
.smoothScrollToPosition(index
);
242 else if (android
.os
.Build
.VERSION
.SDK_INT
>= 8)
244 list
.setSelectionFromTop(index
, 0);
252 * Save index and top position
254 private void saveIndexAndTopPosition(int index
) {
258 ExtendedListView list
= (ExtendedListView
) getListView();
260 int firstPosition
= list
.getFirstVisiblePosition();
261 mFirstPositions
.add(firstPosition
);
263 View view
= list
.getChildAt(0);
264 int top
= (view
== null
) ?
0 : view
.getTop() ;
268 // Save the height of a cell
269 mHeightCell
= (view
== null
|| mHeightCell
!= 0) ? mHeightCell
: view
.getHeight();
273 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
274 OCFile file
= mContainerActivity
.getStorageManager().createFileInstance(
275 (Cursor
) mAdapter
.getItem(position
));
277 if (file
.isFolder()) {
278 // update state and view of this fragment
280 // then, notify parent activity to let it update its state and view, and other fragments
281 mContainerActivity
.onBrowsedDownTo(file
);
282 // save index and top position
283 saveIndexAndTopPosition(position
);
285 } else { /// Click on a file
286 if (PreviewImageFragment
.canBePreviewed(file
)) {
287 // preview image - it handles the download, if needed
288 ((FileDisplayActivity
)mContainerActivity
).startImagePreview(file
);
290 } else if (file
.isDown()) {
291 if (PreviewMediaFragment
.canBePreviewed(file
)) {
293 ((FileDisplayActivity
)mContainerActivity
).startMediaPreview(file
, 0, true
);
295 ((FileDisplayActivity
)mContainerActivity
).getFileOperationsHelper().openFile(file
);
299 // automatic download, preview on finish
300 ((FileDisplayActivity
)mContainerActivity
).startDownloadForPreview(file
);
306 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
315 public void onCreateContextMenu (ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
316 super.onCreateContextMenu(menu
, v
, menuInfo
);
317 MenuInflater inflater
= getSherlockActivity().getMenuInflater();
318 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
319 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
320 OCFile targetFile
= mContainerActivity
.getStorageManager().createFileInstance(
321 (Cursor
) mAdapter
.getItem(info
.position
));
322 List
<Integer
> toHide
= new ArrayList
<Integer
>();
323 List
<Integer
> toDisable
= new ArrayList
<Integer
>();
325 MenuItem item
= null
;
326 if (targetFile
.isFolder()) {
327 // contextual menu for folders
328 toHide
.add(R
.id
.action_open_file_with
);
329 toHide
.add(R
.id
.action_download_file
);
330 toHide
.add(R
.id
.action_cancel_download
);
331 toHide
.add(R
.id
.action_cancel_upload
);
332 toHide
.add(R
.id
.action_sync_file
);
333 toHide
.add(R
.id
.action_see_details
);
334 toHide
.add(R
.id
.action_send_file
);
335 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile
) ||
336 mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile
) ) {
337 toDisable
.add(R
.id
.action_rename_file
);
338 toDisable
.add(R
.id
.action_remove_file
);
343 // contextual menu for regular files
345 // new design: 'download' and 'open with' won't be available anymore in context menu
346 toHide
.add(R
.id
.action_download_file
);
347 toHide
.add(R
.id
.action_open_file_with
);
349 if (targetFile
.isDown()) {
350 toHide
.add(R
.id
.action_cancel_download
);
351 toHide
.add(R
.id
.action_cancel_upload
);
354 toHide
.add(R
.id
.action_sync_file
);
356 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile
)) {
357 toHide
.add(R
.id
.action_cancel_upload
);
358 toDisable
.add(R
.id
.action_rename_file
);
359 toDisable
.add(R
.id
.action_remove_file
);
361 } else if ( mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile
)) {
362 toHide
.add(R
.id
.action_cancel_download
);
363 toDisable
.add(R
.id
.action_rename_file
);
364 toDisable
.add(R
.id
.action_remove_file
);
367 toHide
.add(R
.id
.action_cancel_download
);
368 toHide
.add(R
.id
.action_cancel_upload
);
373 if (!targetFile
.isShareByLink()) {
374 toHide
.add(R
.id
.action_unshare_file
);
378 boolean sendEnabled
= getString(R
.string
.send_files_to_other_apps
).equalsIgnoreCase("on");
380 toHide
.add(R
.id
.action_send_file
);
383 for (int i
: toHide
) {
384 item
= menu
.findItem(i
);
386 item
.setVisible(false
);
387 item
.setEnabled(false
);
391 for (int i
: toDisable
) {
392 item
= menu
.findItem(i
);
394 item
.setEnabled(false
);
404 public boolean onContextItemSelected (MenuItem item
) {
405 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
406 mTargetFile
= mContainerActivity
.getStorageManager().createFileInstance(
407 (Cursor
) mAdapter
.getItem(info
.position
));
408 switch (item
.getItemId()) {
409 case R
.id
.action_share_file
: {
410 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
413 case R
.id
.action_unshare_file
: {
414 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
417 case R
.id
.action_rename_file
: {
418 String fileName
= mTargetFile
.getFileName();
419 int extensionStart
= mTargetFile
.isFolder() ?
-1 : fileName
.lastIndexOf(".");
420 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: fileName
.length();
421 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), fileName
, 0, selectionEnd
, this);
422 dialog
.show(getFragmentManager(), EditNameDialog
.TAG
);
425 case R
.id
.action_remove_file
: {
426 int messageStringId
= R
.string
.confirmation_remove_alert
;
427 int posBtnStringId
= R
.string
.confirmation_remove_remote
;
428 int neuBtnStringId
= -1;
429 if (mTargetFile
.isFolder()) {
430 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
431 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
432 neuBtnStringId
= R
.string
.confirmation_remove_folder_local
;
433 } else if (mTargetFile
.isDown()) {
434 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
435 neuBtnStringId
= R
.string
.confirmation_remove_local
;
437 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
439 new String
[]{mTargetFile
.getFileName()},
442 R
.string
.common_cancel
);
443 confDialog
.setOnConfirmationListener(this);
444 confDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
447 case R
.id
.action_sync_file
: {
448 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
451 case R
.id
.action_cancel_download
: {
452 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
453 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
454 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, mTargetFile
)) {
455 downloaderBinder
.cancel(account
, mTargetFile
);
457 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
461 case R
.id
.action_cancel_upload
: {
462 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
463 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
464 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, mTargetFile
)) {
465 uploaderBinder
.cancel(account
, mTargetFile
);
467 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
471 case R
.id
.action_see_details
: {
472 mContainerActivity
.showDetails(mTargetFile
);
475 case R
.id
.action_send_file
: {
477 if (!mTargetFile
.isDown()) { // Download the file
478 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
479 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(mTargetFile
);
482 ((FileDisplayActivity
)mContainerActivity
).getFileOperationsHelper().sendDownloadedFile(mTargetFile
);
487 return super.onContextItemSelected(item
);
493 * Use this to query the {@link OCFile} that is currently
494 * being displayed by this fragment
495 * @return The currently viewed OCFile
497 public OCFile
getCurrentFile(){
502 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
504 public void listDirectory(){
509 * Lists the given directory on the view. When the input parameter is null,
510 * it will either refresh the last known directory. list the root
511 * if there never was a directory.
513 * @param directory File to be listed
515 public void listDirectory(OCFile directory
) {
516 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
517 if (storageManager
!= null
) {
519 // Check input parameters for null
520 if(directory
== null
){
524 directory
= storageManager
.getFileByPath("/");
525 if (directory
== null
) return; // no files, wait for sync
530 // If that's not a directory -> List its parent
531 if(!directory
.isFolder()){
532 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
533 directory
= storageManager
.getFileById(directory
.getParentId());
536 swapDirectory(directory
.getFileId(), storageManager
);
538 if (mFile
== null
|| !mFile
.equals(directory
)) {
539 ((ExtendedListView
) getListView()).setSelectionFromTop(0, 0);
547 * Change the adapted directory for a new one
548 * @param folder New file to adapt. Can be NULL, meaning "no content to adapt".
549 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
551 public void swapDirectory(long parentId
, FileDataStorageManager updatedStorageManager
) {
552 FileDataStorageManager storageManager
= null
;
553 if (updatedStorageManager
!= null
&& updatedStorageManager
!= storageManager
) {
554 storageManager
= updatedStorageManager
;
556 Cursor newCursor
= null
;
557 if (storageManager
!= null
) {
558 mAdapter
.setStorageManager(storageManager
);
559 mCursorLoader
.setParentId(parentId
);
560 mCursorLoader
.setStorageManager(storageManager
);
561 newCursor
= mCursorLoader
.loadInBackground();//storageManager.getContent(folder.getFileId());
562 Uri uri
= Uri
.withAppendedPath(
563 ProviderTableMeta
.CONTENT_URI_DIR
,
564 String
.valueOf(parentId
));
565 Log_OC
.d(TAG
, "swapDirectory Uri " + uri
);
566 //newCursor.setNotificationUri(getSherlockActivity().getContentResolver(), uri);
569 Cursor oldCursor
= mAdapter
.swapCursor(newCursor
);
570 if (oldCursor
!= null
){
573 mAdapter
.notifyDataSetChanged();
578 public void onDismiss(EditNameDialog dialog
) {
579 if (dialog
.getResult()) {
580 String newFilename
= dialog
.getNewFilename();
581 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
582 mContainerActivity
.getFileOperationsHelper().renameFile(mTargetFile
, newFilename
);
588 public void onConfirmation(String callerTag
) {
589 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
590 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
591 if (storageManager
.getFileById(mTargetFile
.getFileId()) != null
) {
592 mContainerActivity
.getFileOperationsHelper().removeFile(mTargetFile
, true
);
598 public void onNeutral(String callerTag
) {
599 mContainerActivity
.getStorageManager().removeFile(mTargetFile
, false
, true
); // TODO perform in background task / new thread
601 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
605 public void onCancel(String callerTag
) {
606 Log_OC
.d(TAG
, "REMOVAL CANCELED");
610 * LoaderManager.LoaderCallbacks<Cursor>
614 * Instantiate and return a new Loader for the given ID. This is where the cursor is created.
617 public Loader
<Cursor
> onCreateLoader(int id
, Bundle bundle
) {
618 Log_OC
.d(TAG
, "onCreateLoader start");
619 mCursorLoader
= new FileListCursorLoader(
620 getSherlockActivity(),
621 mContainerActivity
.getStorageManager());
623 mCursorLoader
.setParentId(mFile
.getFileId());
625 mCursorLoader
.setParentId(1);
627 Log_OC
.d(TAG
, "onCreateLoader end");
628 return mCursorLoader
;
633 * Called when a previously created loader has finished its load. Here, you can start using the cursor.
636 public void onLoadFinished(Loader
<Cursor
> loader
, Cursor cursor
) {
637 Log_OC
.d(TAG
, "onLoadFinished start");
639 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
640 if (storageManager
!= null
) {
641 mCursorLoader
.setStorageManager(storageManager
);
643 mCursorLoader
.setParentId(mFile
.getFileId());
645 mCursorLoader
.setParentId(1);
647 mAdapter
.swapCursor(mCursorLoader
.loadInBackground());
650 // if(mAdapter != null && cursor != null)
651 // mAdapter.swapCursor(cursor); //swap the new cursor in.
653 // Log_OC.d(TAG,"OnLoadFinished: mAdapter is null");
655 Log_OC
.d(TAG
, "onLoadFinished end");
660 * Called when a previously created loader is being reset, thus making its data unavailable.
661 * It is being reset in order to create a new cursor to query different data.
662 * This is called when the last Cursor provided to onLoadFinished() above is about to be closed.
663 * We need to make sure we are no longer using it.
666 public void onLoaderReset(Loader
<Cursor
> loader
) {
667 Log_OC
.d(TAG
, "onLoadReset start");
669 mAdapter
.swapCursor(null
);
671 Log_OC
.d(TAG
,"OnLoadFinished: mAdapter is null");
672 Log_OC
.d(TAG
, "onLoadReset end");