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 mHeightCell
= savedInstanceState
.getInt(KEY_HEIGHT_CELL
);
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
);
171 * Call this, when the user presses the up button.
173 * Tries to move up the current folder one level. If the parent folder was removed from the database,
174 * it continues browsing up until finding an existing folders.
176 * return Count of folder levels browsed up.
178 public int onBrowseUp() {
179 OCFile parentDir
= null
;
183 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
185 String parentPath
= null
;
186 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
187 parentPath
= new File(mFile
.getRemotePath()).getParent();
188 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
189 parentDir
= storageManager
.getFileByPath(parentPath
);
192 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
); // never returns null; keep the path in root folder
194 while (parentDir
== null
) {
195 parentPath
= new File(parentPath
).getParent();
196 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
197 parentDir
= storageManager
.getFileByPath(parentPath
);
199 } // exit is granted because storageManager.getFileByPath("/") never returns null
204 listDirectory(mFile
);
206 ((FileDisplayActivity
)mContainerActivity
).startSyncFolderOperation(mFile
);
208 // restore index and top position
209 restoreIndexAndTopPosition();
211 } // else - should never happen now
217 * Restore index and position
219 private void restoreIndexAndTopPosition() {
220 if (mIndexes
.size() > 0) {
221 // needs to be checked; not every browse-up had a browse-down before
223 int index
= mIndexes
.remove(mIndexes
.size() - 1);
225 int firstPosition
= mFirstPositions
.remove(mFirstPositions
.size() -1);
227 int top
= mTops
.remove(mTops
.size() - 1);
229 ExtendedListView list
= (ExtendedListView
) getListView();
230 list
.setSelectionFromTop(firstPosition
, top
);
232 // Move the scroll if the selection is not visible
233 int indexPosition
= mHeightCell
*index
;
234 int height
= list
.getHeight();
236 if (indexPosition
> height
) {
237 if (android
.os
.Build
.VERSION
.SDK_INT
>= 11)
239 list
.smoothScrollToPosition(index
);
241 else if (android
.os
.Build
.VERSION
.SDK_INT
>= 8)
243 list
.setSelectionFromTop(index
, 0);
251 * Save index and top position
253 private void saveIndexAndTopPosition(int index
) {
257 ExtendedListView list
= (ExtendedListView
) getListView();
259 int firstPosition
= list
.getFirstVisiblePosition();
260 mFirstPositions
.add(firstPosition
);
262 View view
= list
.getChildAt(0);
263 int top
= (view
== null
) ?
0 : view
.getTop() ;
267 // Save the height of a cell
268 mHeightCell
= (view
== null
|| mHeightCell
!= 0) ? mHeightCell
: view
.getHeight();
272 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
273 OCFile file
= mContainerActivity
.getStorageManager().createFileInstance(
274 (Cursor
) mAdapter
.getItem(position
));
276 if (file
.isFolder()) {
277 // update state and view of this fragment
279 // then, notify parent activity to let it update its state and view, and other fragments
280 mContainerActivity
.onBrowsedDownTo(file
);
281 // save index and top position
282 saveIndexAndTopPosition(position
);
284 } else { /// Click on a file
285 if (PreviewImageFragment
.canBePreviewed(file
)) {
286 // preview image - it handles the download, if needed
287 ((FileDisplayActivity
)mContainerActivity
).startImagePreview(file
);
289 } else if (file
.isDown()) {
290 if (PreviewMediaFragment
.canBePreviewed(file
)) {
292 ((FileDisplayActivity
)mContainerActivity
).startMediaPreview(file
, 0, true
);
294 ((FileDisplayActivity
)mContainerActivity
).getFileOperationsHelper().openFile(file
);
298 // automatic download, preview on finish
299 ((FileDisplayActivity
)mContainerActivity
).startDownloadForPreview(file
);
305 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
314 public void onCreateContextMenu (ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
315 super.onCreateContextMenu(menu
, v
, menuInfo
);
316 MenuInflater inflater
= getSherlockActivity().getMenuInflater();
317 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
318 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
319 OCFile targetFile
= mContainerActivity
.getStorageManager().createFileInstance(
320 (Cursor
) mAdapter
.getItem(info
.position
));
321 List
<Integer
> toHide
= new ArrayList
<Integer
>();
322 List
<Integer
> toDisable
= new ArrayList
<Integer
>();
324 MenuItem item
= null
;
325 if (targetFile
.isFolder()) {
326 // contextual menu for folders
327 toHide
.add(R
.id
.action_open_file_with
);
328 toHide
.add(R
.id
.action_download_file
);
329 toHide
.add(R
.id
.action_cancel_download
);
330 toHide
.add(R
.id
.action_cancel_upload
);
331 toHide
.add(R
.id
.action_sync_file
);
332 toHide
.add(R
.id
.action_see_details
);
333 toHide
.add(R
.id
.action_send_file
);
334 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile
) ||
335 mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile
) ) {
336 toDisable
.add(R
.id
.action_rename_file
);
337 toDisable
.add(R
.id
.action_remove_file
);
342 // contextual menu for regular files
344 // new design: 'download' and 'open with' won't be available anymore in context menu
345 toHide
.add(R
.id
.action_download_file
);
346 toHide
.add(R
.id
.action_open_file_with
);
348 if (targetFile
.isDown()) {
349 toHide
.add(R
.id
.action_cancel_download
);
350 toHide
.add(R
.id
.action_cancel_upload
);
353 toHide
.add(R
.id
.action_sync_file
);
355 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile
)) {
356 toHide
.add(R
.id
.action_cancel_upload
);
357 toDisable
.add(R
.id
.action_rename_file
);
358 toDisable
.add(R
.id
.action_remove_file
);
360 } else if ( mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile
)) {
361 toHide
.add(R
.id
.action_cancel_download
);
362 toDisable
.add(R
.id
.action_rename_file
);
363 toDisable
.add(R
.id
.action_remove_file
);
366 toHide
.add(R
.id
.action_cancel_download
);
367 toHide
.add(R
.id
.action_cancel_upload
);
372 if (!targetFile
.isShareByLink()) {
373 toHide
.add(R
.id
.action_unshare_file
);
377 boolean sendEnabled
= getString(R
.string
.send_files_to_other_apps
).equalsIgnoreCase("on");
379 toHide
.add(R
.id
.action_send_file
);
382 for (int i
: toHide
) {
383 item
= menu
.findItem(i
);
385 item
.setVisible(false
);
386 item
.setEnabled(false
);
390 for (int i
: toDisable
) {
391 item
= menu
.findItem(i
);
393 item
.setEnabled(false
);
403 public boolean onContextItemSelected (MenuItem item
) {
404 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
405 mTargetFile
= mContainerActivity
.getStorageManager().createFileInstance(
406 (Cursor
) mAdapter
.getItem(info
.position
));
407 switch (item
.getItemId()) {
408 case R
.id
.action_share_file
: {
409 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
412 case R
.id
.action_unshare_file
: {
413 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
416 case R
.id
.action_rename_file
: {
417 String fileName
= mTargetFile
.getFileName();
418 int extensionStart
= mTargetFile
.isFolder() ?
-1 : fileName
.lastIndexOf(".");
419 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: fileName
.length();
420 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), fileName
, 0, selectionEnd
, this);
421 dialog
.show(getFragmentManager(), EditNameDialog
.TAG
);
424 case R
.id
.action_remove_file
: {
425 int messageStringId
= R
.string
.confirmation_remove_alert
;
426 int posBtnStringId
= R
.string
.confirmation_remove_remote
;
427 int neuBtnStringId
= -1;
428 if (mTargetFile
.isFolder()) {
429 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
430 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
431 neuBtnStringId
= R
.string
.confirmation_remove_folder_local
;
432 } else if (mTargetFile
.isDown()) {
433 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
434 neuBtnStringId
= R
.string
.confirmation_remove_local
;
436 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
438 new String
[]{mTargetFile
.getFileName()},
441 R
.string
.common_cancel
);
442 confDialog
.setOnConfirmationListener(this);
443 confDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
446 case R
.id
.action_sync_file
: {
447 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
450 case R
.id
.action_cancel_download
: {
451 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
452 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
453 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, mTargetFile
)) {
454 downloaderBinder
.cancel(account
, mTargetFile
);
456 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
460 case R
.id
.action_cancel_upload
: {
461 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
462 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
463 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, mTargetFile
)) {
464 uploaderBinder
.cancel(account
, mTargetFile
);
466 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
470 case R
.id
.action_see_details
: {
471 mContainerActivity
.showDetails(mTargetFile
);
474 case R
.id
.action_send_file
: {
476 if (!mTargetFile
.isDown()) { // Download the file
477 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
478 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(mTargetFile
);
481 ((FileDisplayActivity
)mContainerActivity
).getFileOperationsHelper().sendDownloadedFile(mTargetFile
);
486 return super.onContextItemSelected(item
);
492 * Use this to query the {@link OCFile} that is currently
493 * being displayed by this fragment
494 * @return The currently viewed OCFile
496 public OCFile
getCurrentFile(){
501 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
503 public void listDirectory(){
508 * Lists the given directory on the view. When the input parameter is null,
509 * it will either refresh the last known directory. list the root
510 * if there never was a directory.
512 * @param directory File to be listed
514 public void listDirectory(OCFile directory
) {
515 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
516 if (storageManager
!= null
) {
518 // Check input parameters for null
519 if(directory
== null
){
523 directory
= storageManager
.getFileByPath("/");
524 if (directory
== null
) return; // no files, wait for sync
529 // If that's not a directory -> List its parent
530 if(!directory
.isFolder()){
531 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
532 directory
= storageManager
.getFileById(directory
.getParentId());
535 swapDirectory(directory
.getFileId(), storageManager
);
537 if (mFile
== null
|| !mFile
.equals(directory
)) {
538 ((ExtendedListView
) getListView()).setSelectionFromTop(0, 0);
546 * Change the adapted directory for a new one
547 * @param folder New file to adapt. Can be NULL, meaning "no content to adapt".
548 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
550 public void swapDirectory(long parentId
, FileDataStorageManager updatedStorageManager
) {
551 FileDataStorageManager storageManager
= null
;
552 if (updatedStorageManager
!= null
&& updatedStorageManager
!= storageManager
) {
553 storageManager
= updatedStorageManager
;
555 Cursor newCursor
= null
;
556 if (storageManager
!= null
) {
557 mAdapter
.setStorageManager(storageManager
);
558 mCursorLoader
.setParentId(parentId
);
559 newCursor
= mCursorLoader
.loadInBackground();//storageManager.getContent(folder.getFileId());
560 Uri uri
= Uri
.withAppendedPath(
561 ProviderTableMeta
.CONTENT_URI_DIR
,
562 String
.valueOf(parentId
));
563 Log_OC
.d(TAG
, "swapDirectory Uri " + uri
);
564 //newCursor.setNotificationUri(getSherlockActivity().getContentResolver(), uri);
567 Cursor oldCursor
= mAdapter
.swapCursor(newCursor
);
568 if (oldCursor
!= null
){
571 mAdapter
.notifyDataSetChanged();
576 public void onDismiss(EditNameDialog dialog
) {
577 if (dialog
.getResult()) {
578 String newFilename
= dialog
.getNewFilename();
579 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
580 mContainerActivity
.getFileOperationsHelper().renameFile(mTargetFile
, newFilename
);
586 public void onConfirmation(String callerTag
) {
587 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
588 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
589 if (storageManager
.getFileById(mTargetFile
.getFileId()) != null
) {
590 mContainerActivity
.getFileOperationsHelper().removeFile(mTargetFile
, true
);
596 public void onNeutral(String callerTag
) {
597 mContainerActivity
.getStorageManager().removeFile(mTargetFile
, false
, true
); // TODO perform in background task / new thread
599 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
603 public void onCancel(String callerTag
) {
604 Log_OC
.d(TAG
, "REMOVAL CANCELED");
608 * LoaderManager.LoaderCallbacks<Cursor>
612 * Instantiate and return a new Loader for the given ID. This is where the cursor is created.
615 public Loader
<Cursor
> onCreateLoader(int id
, Bundle bundle
) {
616 Log_OC
.d(TAG
, "onCreateLoader start");
617 mCursorLoader
= new FileListCursorLoader(
618 getSherlockActivity(),
619 mContainerActivity
.getStorageManager());
621 mCursorLoader
.setParentId(mFile
.getFileId());
623 mCursorLoader
.setParentId(1);
625 Log_OC
.d(TAG
, "onCreateLoader end");
626 return mCursorLoader
;
631 * Called when a previously created loader has finished its load. Here, you can start using the cursor.
634 public void onLoadFinished(Loader
<Cursor
> loader
, Cursor cursor
) {
635 Log_OC
.d(TAG
, "onLoadFinished start");
637 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
638 if (storageManager
!= null
) {
639 mCursorLoader
.setStorageManager(storageManager
);
641 mCursorLoader
.setParentId(mFile
.getFileId());
643 mCursorLoader
.setParentId(1);
645 mAdapter
.swapCursor(mCursorLoader
.loadInBackground());
648 // if(mAdapter != null && cursor != null)
649 // mAdapter.swapCursor(cursor); //swap the new cursor in.
651 // Log_OC.d(TAG,"OnLoadFinished: mAdapter is null");
653 Log_OC
.d(TAG
, "onLoadFinished end");
658 * Called when a previously created loader is being reset, thus making its data unavailable.
659 * It is being reset in order to create a new cursor to query different data.
660 * This is called when the last Cursor provided to onLoadFinished() above is about to be closed.
661 * We need to make sure we are no longer using it.
664 public void onLoaderReset(Loader
<Cursor
> loader
) {
665 Log_OC
.d(TAG
, "onLoadReset start");
667 mAdapter
.swapCursor(null
);
669 Log_OC
.d(TAG
,"OnLoadFinished: mAdapter is null");
670 Log_OC
.d(TAG
, "onLoadReset end");