1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 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
.content
.Intent
;
48 import android
.net
.Uri
;
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 OCFileListFragment
.ContainerActivity mContainerActivity
;
73 private OCFile mFile
= null
;
74 private FileListListAdapter mAdapter
;
76 private Handler mHandler
;
77 private OCFile mTargetFile
;
83 public void onAttach(Activity activity
) {
84 super.onAttach(activity
);
85 Log_OC
.e(TAG
, "onAttach");
87 mContainerActivity
= (ContainerActivity
) activity
;
88 } catch (ClassCastException e
) {
89 throw new ClassCastException(activity
.toString() + " must implement " + OCFileListFragment
.ContainerActivity
.class.getSimpleName());
98 public void onActivityCreated(Bundle savedInstanceState
) {
99 super.onActivityCreated(savedInstanceState
);
100 Log_OC
.e(TAG
, "onActivityCreated() start");
101 mAdapter
= new FileListListAdapter(getActivity(), mContainerActivity
);
102 if (savedInstanceState
!= null
) {
103 mFile
= savedInstanceState
.getParcelable(EXTRA_FILE
);
105 setListAdapter(mAdapter
);
107 registerForContextMenu(getListView());
108 getListView().setOnCreateContextMenuListener(this);
110 mHandler
= new Handler();
115 * Saves the current listed folder.
118 public void onSaveInstanceState (Bundle outState
) {
119 super.onSaveInstanceState(outState
);
120 outState
.putParcelable(EXTRA_FILE
, mFile
);
125 * Call this, when the user presses the up button.
127 * Tries to move up the current folder one level. If the parent folder was removed from the database,
128 * it continues browsing up until finding an existing folders.
130 * return Count of folder levels browsed up.
132 public int onBrowseUp() {
133 OCFile parentDir
= null
;
137 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
139 String parentPath
= null
;
140 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
141 parentPath
= new File(mFile
.getRemotePath()).getParent();
142 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
143 parentDir
= storageManager
.getFileByPath(parentPath
);
146 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
); // never returns null; keep the path in root folder
148 while (parentDir
== null
) {
149 parentPath
= new File(parentPath
).getParent();
150 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
151 parentDir
= storageManager
.getFileByPath(parentPath
);
153 } // exit is granted because storageManager.getFileByPath("/") never returns null
158 listDirectory(mFile
);
160 mContainerActivity
.startSyncFolderOperation(mFile
);
161 } // else - should never happen now
167 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
168 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
170 if (file
.isFolder()) {
171 // update state and view of this fragment
173 // then, notify parent activity to let it update its state and view, and other fragments
174 mContainerActivity
.onBrowsedDownTo(file
);
176 } else { /// Click on a file
177 if (PreviewImageFragment
.canBePreviewed(file
)) {
178 // preview image - it handles the download, if needed
179 mContainerActivity
.startImagePreview(file
);
181 } else if (file
.isDown()) {
182 if (PreviewMediaFragment
.canBePreviewed(file
)) {
184 mContainerActivity
.startMediaPreview(file
, 0, true
);
186 FileDisplayActivity activity
= (FileDisplayActivity
) getSherlockActivity();
187 activity
.getFileOperationsHelper().openFile(file
, activity
);
191 // automatic download, preview on finish
192 mContainerActivity
.startDownloadForPreview(file
);
198 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
207 public void onCreateContextMenu (ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
208 super.onCreateContextMenu(menu
, v
, menuInfo
);
209 MenuInflater inflater
= getActivity().getMenuInflater();
210 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
211 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
212 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
213 List
<Integer
> toHide
= new ArrayList
<Integer
>();
214 List
<Integer
> toDisable
= new ArrayList
<Integer
>();
216 MenuItem item
= null
;
217 if (targetFile
.isFolder()) {
218 // contextual menu for folders
219 toHide
.add(R
.id
.action_open_file_with
);
220 toHide
.add(R
.id
.action_download_file
);
221 toHide
.add(R
.id
.action_cancel_download
);
222 toHide
.add(R
.id
.action_cancel_upload
);
223 toHide
.add(R
.id
.action_sync_file
);
224 toHide
.add(R
.id
.action_see_details
);
225 toHide
.add(R
.id
.action_share_file
);
226 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
) ||
227 mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
) ) {
228 toDisable
.add(R
.id
.action_rename_file
);
229 toDisable
.add(R
.id
.action_remove_file
);
234 // contextual menu for regular files
236 // new design: 'download' and 'open with' won't be available anymore in context menu
237 toHide
.add(R
.id
.action_download_file
);
238 toHide
.add(R
.id
.action_open_file_with
);
240 if (targetFile
.isDown()) {
241 toHide
.add(R
.id
.action_cancel_download
);
242 toHide
.add(R
.id
.action_cancel_upload
);
245 toHide
.add(R
.id
.action_sync_file
);
247 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
248 toHide
.add(R
.id
.action_cancel_upload
);
249 toDisable
.add(R
.id
.action_rename_file
);
250 toDisable
.add(R
.id
.action_remove_file
);
252 } else if ( mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
253 toHide
.add(R
.id
.action_cancel_download
);
254 toDisable
.add(R
.id
.action_rename_file
);
255 toDisable
.add(R
.id
.action_remove_file
);
258 toHide
.add(R
.id
.action_cancel_download
);
259 toHide
.add(R
.id
.action_cancel_upload
);
264 if (!targetFile
.isShareByLink()) {
265 toHide
.add(R
.id
.action_unshare_file
);
268 for (int i
: toHide
) {
269 item
= menu
.findItem(i
);
271 item
.setVisible(false
);
272 item
.setEnabled(false
);
276 for (int i
: toDisable
) {
277 item
= menu
.findItem(i
);
279 item
.setEnabled(false
);
289 public boolean onContextItemSelected (MenuItem item
) {
290 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
291 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
292 switch (item
.getItemId()) {
293 case R
.id
.action_share_file
: {
294 FileDisplayActivity activity
= (FileDisplayActivity
) getSherlockActivity();
295 activity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
, activity
);
298 case R
.id
.action_unshare_file
: {
299 FileDisplayActivity activity
= (FileDisplayActivity
) getSherlockActivity();
300 activity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
, activity
);
303 case R
.id
.action_rename_file
: {
304 String fileName
= mTargetFile
.getFileName();
305 int extensionStart
= mTargetFile
.isFolder() ?
-1 : fileName
.lastIndexOf(".");
306 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: fileName
.length();
307 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), fileName
, 0, selectionEnd
, this);
308 dialog
.show(getFragmentManager(), EditNameDialog
.TAG
);
311 case R
.id
.action_remove_file
: {
312 int messageStringId
= R
.string
.confirmation_remove_alert
;
313 int posBtnStringId
= R
.string
.confirmation_remove_remote
;
314 int neuBtnStringId
= -1;
315 if (mTargetFile
.isFolder()) {
316 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
317 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
318 neuBtnStringId
= R
.string
.confirmation_remove_folder_local
;
319 } else if (mTargetFile
.isDown()) {
320 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
321 neuBtnStringId
= R
.string
.confirmation_remove_local
;
323 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
325 new String
[]{mTargetFile
.getFileName()},
328 R
.string
.common_cancel
);
329 confDialog
.setOnConfirmationListener(this);
330 confDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
333 case R
.id
.action_sync_file
: {
334 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
335 RemoteOperation operation
= new SynchronizeFileOperation(mTargetFile
, null
, mContainerActivity
.getStorageManager(), account
, true
, getSherlockActivity());
336 operation
.execute(account
, getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
337 ((FileDisplayActivity
) getSherlockActivity()).showLoadingDialog();
340 case R
.id
.action_cancel_download
: {
341 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
342 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
343 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, mTargetFile
)) {
344 downloaderBinder
.cancel(account
, mTargetFile
);
346 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
350 case R
.id
.action_cancel_upload
: {
351 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
352 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
353 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, mTargetFile
)) {
354 uploaderBinder
.cancel(account
, mTargetFile
);
356 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
360 case R
.id
.action_see_details
: {
361 ((FileFragment
.ContainerActivity
)getActivity()).showDetails(mTargetFile
);
364 case R
.id
.action_send_file
: {
365 Intent sharingIntent
= new Intent(android
.content
.Intent
.ACTION_SEND
);
367 sharingIntent
.setType(mTargetFile
.getMimetype());
368 sharingIntent
.putExtra(Intent
.EXTRA_STREAM
, Uri
.parse("file://"+mTargetFile
.getStoragePath()));
369 startActivity(Intent
.createChooser(sharingIntent
, "Share via"));
373 return super.onContextItemSelected(item
);
379 * Use this to query the {@link OCFile} that is currently
380 * being displayed by this fragment
381 * @return The currently viewed OCFile
383 public OCFile
getCurrentFile(){
388 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
390 public void listDirectory(){
395 * Lists the given directory on the view. When the input parameter is null,
396 * it will either refresh the last known directory. list the root
397 * if there never was a directory.
399 * @param directory File to be listed
401 public void listDirectory(OCFile directory
) {
402 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
403 if (storageManager
!= null
) {
405 // Check input parameters for null
406 if(directory
== null
){
410 directory
= storageManager
.getFileByPath("/");
411 if (directory
== null
) return; // no files, wait for sync
416 // If that's not a directory -> List its parent
417 if(!directory
.isFolder()){
418 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
419 directory
= storageManager
.getFileById(directory
.getParentId());
422 mAdapter
.swapDirectory(directory
, storageManager
);
423 if (mFile
== null
|| !mFile
.equals(directory
)) {
424 mList
.setSelectionFromTop(0, 0);
433 * Interface to implement by any Activity that includes some instance of FileListFragment
435 * @author David A. Velasco
437 public interface ContainerActivity
extends TransferServiceGetter
, OnRemoteOperationListener
{
440 * Callback method invoked when a the user browsed into a different folder through the list of files
444 public void onBrowsedDownTo(OCFile folder
);
446 public void startDownloadForPreview(OCFile file
);
448 public void startMediaPreview(OCFile file
, int i
, boolean b
);
450 public void startImagePreview(OCFile file
);
452 public void startSyncFolderOperation(OCFile folder
);
455 * Getter for the current DataStorageManager in the container activity
457 public FileDataStorageManager
getStorageManager();
461 * Callback method invoked when a the 'transfer state' of a file changes.
463 * This happens when a download or upload is started or ended for a file.
465 * This method is necessary by now to update the user interface of the double-pane layout in tablets
466 * because methods {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and {@link FileUploaderBinder#isUploading(Account, OCFile)}
467 * won't provide the needed response before the method where this is called finishes.
469 * TODO Remove this when the transfer state of a file is kept in the database (other thing TODO)
471 * @param file OCFile which state changed.
472 * @param downloading Flag signaling if the file is now downloading.
473 * @param uploading Flag signaling if the file is now uploading.
475 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
);
481 public void onDismiss(EditNameDialog dialog
) {
482 if (dialog
.getResult()) {
483 String newFilename
= dialog
.getNewFilename();
484 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
485 RemoteOperation operation
= new RenameFileOperation(mTargetFile
,
486 AccountUtils
.getCurrentOwnCloudAccount(getActivity()),
488 mContainerActivity
.getStorageManager());
489 operation
.execute(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
490 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
496 public void onConfirmation(String callerTag
) {
497 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
498 if (mContainerActivity
.getStorageManager().getFileById(mTargetFile
.getFileId()) != null
) {
499 RemoteOperation operation
= new RemoveFileOperation( mTargetFile
,
501 mContainerActivity
.getStorageManager());
502 operation
.execute(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
504 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
510 public void onNeutral(String callerTag
) {
511 mContainerActivity
.getStorageManager().removeFile(mTargetFile
, false
, true
); // TODO perform in background task / new thread
513 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
517 public void onCancel(String callerTag
) {
518 Log_OC
.d(TAG
, "REMOVAL CANCELED");