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
;
23 import java
.util
.Vector
;
25 import android
.accounts
.Account
;
26 import android
.app
.Activity
;
27 import android
.content
.Context
;
28 import android
.os
.Bundle
;
29 import android
.os
.Handler
;
30 import android
.view
.ContextMenu
;
31 import android
.view
.LayoutInflater
;
32 import android
.view
.MenuInflater
;
33 import android
.view
.MenuItem
;
34 import android
.view
.View
;
35 import android
.widget
.AdapterView
;
36 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
37 import android
.widget
.TextView
;
39 import com
.owncloud
.android
.Log_OC
;
40 import com
.owncloud
.android
.R
;
41 import com
.owncloud
.android
.authentication
.AccountUtils
;
42 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
43 import com
.owncloud
.android
.datamodel
.OCFile
;
44 import com
.owncloud
.android
.files
.FileHandler
;
45 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
46 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
47 import com
.owncloud
.android
.operations
.OnRemoteOperationListener
;
48 import com
.owncloud
.android
.operations
.RemoteOperation
;
49 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
50 import com
.owncloud
.android
.operations
.RenameFileOperation
;
51 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
52 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
53 import com
.owncloud
.android
.ui
.activity
.TransferServiceGetter
;
54 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
55 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
56 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
57 import com
.owncloud
.android
.ui
.fragment
.ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
;
58 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
59 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
62 * A Fragment that lists all files and folders in a given path.
64 * @author Bartek Przybylski
67 public class OCFileListFragment
extends ExtendedListFragment
implements EditNameDialogListener
,
68 ConfirmationDialogFragmentListener
{
70 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
72 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ? OCFileListFragment
.class
73 .getPackage().getName() : "com.owncloud.android.ui.fragment";
74 private static final String EXTRA_FILE
= MY_PACKAGE
+ ".extra.FILE";
76 private OCFileListFragment
.ContainerActivity mContainerActivity
;
78 private OCFile mFile
= null
;
79 private FileListListAdapter mAdapter
;
80 private View mFooterView
;
82 private Handler mHandler
;
83 private OCFile mTargetFile
;
89 public void onAttach(Activity activity
) {
90 super.onAttach(activity
);
91 Log_OC
.e(TAG
, "onAttach");
93 mContainerActivity
= (ContainerActivity
) activity
;
94 } catch (ClassCastException e
) {
95 throw new ClassCastException(activity
.toString() + " must implement "
96 + OCFileListFragment
.ContainerActivity
.class.getSimpleName());
104 public void onActivityCreated(Bundle savedInstanceState
) {
105 super.onActivityCreated(savedInstanceState
);
106 Log_OC
.e(TAG
, "onActivityCreated() start");
108 mFooterView
= ((LayoutInflater
) getActivity().getSystemService(Context
.LAYOUT_INFLATER_SERVICE
)).inflate(
109 R
.layout
.list_footer
, null
, false
);
110 setFooterView(mFooterView
);
112 mAdapter
= new FileListListAdapter(getActivity(), mContainerActivity
);
113 if (savedInstanceState
!= null
) {
114 mFile
= savedInstanceState
.getParcelable(EXTRA_FILE
);
116 setListAdapter(mAdapter
);
118 registerForContextMenu(getListView());
119 getListView().setOnCreateContextMenuListener(this);
121 mHandler
= new Handler();
126 * Saves the current listed folder.
129 public void onSaveInstanceState(Bundle outState
) {
130 super.onSaveInstanceState(outState
);
131 outState
.putParcelable(EXTRA_FILE
, mFile
);
135 * Call this, when the user presses the up button.
137 * Tries to move up the current folder one level. If the parent folder was
138 * removed from the database, it continues browsing up until finding an
141 * return Count of folder levels browsed up.
143 public int onBrowseUp() {
144 OCFile parentDir
= null
;
148 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
150 String parentPath
= null
;
151 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
152 parentPath
= new File(mFile
.getRemotePath()).getParent();
153 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
154 + OCFile
.PATH_SEPARATOR
;
155 parentDir
= storageManager
.getFileByPath(parentPath
);
158 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
); // never
168 while (parentDir
== null
) {
169 parentPath
= new File(parentPath
).getParent();
170 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
171 + OCFile
.PATH_SEPARATOR
;
172 parentDir
= storageManager
.getFileByPath(parentPath
);
174 } // exit is granted because storageManager.getFileByPath("/") never
180 listDirectory(mFile
);
182 mContainerActivity
.startSyncFolderOperation(mFile
);
183 } // else - should never happen now
189 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
190 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
192 if (file
.isFolder()) {
193 // update state and view of this fragment
195 // then, notify parent activity to let it update its state and
196 // view, and other fragments
197 mContainerActivity
.onBrowsedDownTo(file
);
199 } else { // / Click on a file
200 if (PreviewImageFragment
.canBePreviewed(file
)) {
201 // preview image - it handles the download, if needed
202 mContainerActivity
.startImagePreview(file
);
204 } else if (file
.isDown()) {
205 if (PreviewMediaFragment
.canBePreviewed(file
)) {
207 mContainerActivity
.startMediaPreview(file
, 0, true
);
210 mContainerActivity
.openFile(file
);
214 // automatic download, preview on finish
215 mContainerActivity
.startDownloadForPreview(file
);
221 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
230 public void onCreateContextMenu(ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
231 super.onCreateContextMenu(menu
, v
, menuInfo
);
232 MenuInflater inflater
= getActivity().getMenuInflater();
233 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
234 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
235 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
236 List
<Integer
> toHide
= new ArrayList
<Integer
>();
237 List
<Integer
> toDisable
= new ArrayList
<Integer
>();
239 MenuItem item
= null
;
240 if (targetFile
.isFolder()) {
241 // contextual menu for folders
242 toHide
.add(R
.id
.action_open_file_with
);
243 toHide
.add(R
.id
.action_download_file
);
244 toHide
.add(R
.id
.action_cancel_download
);
245 toHide
.add(R
.id
.action_cancel_upload
);
246 toHide
.add(R
.id
.action_sync_file
);
247 toHide
.add(R
.id
.action_see_details
);
248 if (mContainerActivity
.getFileDownloaderBinder().isDownloading(
249 AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)
250 || mContainerActivity
.getFileUploaderBinder().isUploading(
251 AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
252 toDisable
.add(R
.id
.action_rename_file
);
253 toDisable
.add(R
.id
.action_remove_file
);
258 // contextual menu for regular files
260 // new design: 'download' and 'open with' won't be available anymore
262 toHide
.add(R
.id
.action_download_file
);
263 toHide
.add(R
.id
.action_open_file_with
);
265 if (targetFile
.isDown()) {
266 toHide
.add(R
.id
.action_cancel_download
);
267 toHide
.add(R
.id
.action_cancel_upload
);
270 toHide
.add(R
.id
.action_sync_file
);
272 if (mContainerActivity
.getFileDownloaderBinder().isDownloading(
273 AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
274 toHide
.add(R
.id
.action_cancel_upload
);
275 toDisable
.add(R
.id
.action_rename_file
);
276 toDisable
.add(R
.id
.action_remove_file
);
278 } else if (mContainerActivity
.getFileUploaderBinder().isUploading(
279 AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
280 toHide
.add(R
.id
.action_cancel_download
);
281 toDisable
.add(R
.id
.action_rename_file
);
282 toDisable
.add(R
.id
.action_remove_file
);
285 toHide
.add(R
.id
.action_cancel_download
);
286 toHide
.add(R
.id
.action_cancel_upload
);
290 for (int i
: toHide
) {
291 item
= menu
.findItem(i
);
293 item
.setVisible(false
);
294 item
.setEnabled(false
);
298 for (int i
: toDisable
) {
299 item
= menu
.findItem(i
);
301 item
.setEnabled(false
);
310 public boolean onContextItemSelected(MenuItem item
) {
311 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
312 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
313 switch (item
.getItemId()) {
314 case R
.id
.action_rename_file
: {
315 String fileName
= mTargetFile
.getFileName();
316 int extensionStart
= mTargetFile
.isFolder() ?
-1 : fileName
.lastIndexOf(".");
317 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: fileName
.length();
318 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), fileName
, 0,
320 dialog
.show(getFragmentManager(), EditNameDialog
.TAG
);
323 case R
.id
.action_remove_file
: {
324 int messageStringId
= R
.string
.confirmation_remove_alert
;
325 int posBtnStringId
= R
.string
.confirmation_remove_remote
;
326 int neuBtnStringId
= -1;
327 if (mTargetFile
.isFolder()) {
328 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
329 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
330 neuBtnStringId
= R
.string
.confirmation_remove_folder_local
;
331 } else if (mTargetFile
.isDown()) {
332 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
333 neuBtnStringId
= R
.string
.confirmation_remove_local
;
335 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(messageStringId
,
336 new String
[] { mTargetFile
.getFileName() }, posBtnStringId
, neuBtnStringId
, R
.string
.common_cancel
);
337 confDialog
.setOnConfirmationListener(this);
338 confDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
341 case R
.id
.action_sync_file
: {
342 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
343 RemoteOperation operation
= new SynchronizeFileOperation(mTargetFile
, null
,
344 mContainerActivity
.getStorageManager(), account
, true
, getSherlockActivity());
345 operation
.execute(account
, getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
346 ((FileDisplayActivity
) getSherlockActivity()).showLoadingDialog();
349 case R
.id
.action_cancel_download
: {
350 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
351 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
352 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, mTargetFile
)) {
353 downloaderBinder
.cancel(account
, mTargetFile
);
355 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
359 case R
.id
.action_cancel_upload
: {
360 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
361 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
362 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, mTargetFile
)) {
363 uploaderBinder
.cancel(account
, mTargetFile
);
365 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
369 case R
.id
.action_see_details
: {
370 ((FileFragment
.ContainerActivity
) getActivity()).showDetails(mTargetFile
);
374 return super.onContextItemSelected(item
);
379 * Use this to query the {@link OCFile} that is currently being displayed by
382 * @return The currently viewed OCFile
384 public OCFile
getCurrentFile() {
389 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null
392 public void listDirectory() {
397 * Lists the given directory on the view. When the input parameter is null,
398 * it will either refresh the last known directory. list the root if there
399 * never was a directory.
401 * @param directory File to be listed
403 public void listDirectory(OCFile directory
) {
404 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
405 if (storageManager
!= null
) {
407 // Check input parameters for null
408 if (directory
== null
) {
412 directory
= storageManager
.getFileByPath("/");
413 if (directory
== null
)
414 return; // no files, wait for sync
418 // If that's not a directory -> List its parent
419 if (!directory
.isFolder()) {
420 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
421 directory
= storageManager
.getFileById(directory
.getParentId());
424 mAdapter
.swapDirectory(directory
, storageManager
);
425 if (mFile
== null
|| !mFile
.equals(directory
)) {
426 mList
.setSelectionFromTop(0, 0);
431 TextView footerText
= (TextView
) mFooterView
.findViewById(R
.id
.footerText
);
432 Log_OC
.d("footer", String
.valueOf(System
.currentTimeMillis()));
433 footerText
.setText(generateFooterText(directory
));
434 Log_OC
.d("footer", String
.valueOf(System
.currentTimeMillis()));
438 private String
generateFooterText(OCFile directory
) {
442 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
443 Vector
<OCFile
> mFiles
= storageManager
.getFolderContent(mFile
);
445 for (OCFile ocFile
: mFiles
) {
446 if (ocFile
.isFolder()) {
456 output
= folders
.toString() + " " + getResources().getString(R
.string
.file_list_folder
);
457 } else if (folders
> 1) {
458 output
= folders
.toString() + " " + getResources().getString(R
.string
.file_list_folders
);
461 output
= output
+ " " + files
.toString() + " " + getResources().getString(R
.string
.file_list_file
);
463 output
= output
+ " " + files
.toString() + " " + getResources().getString(R
.string
.file_list_files
);
469 * Interface to implement by any Activity that includes some instance of
472 * @author David A. Velasco
474 public interface ContainerActivity
extends TransferServiceGetter
, OnRemoteOperationListener
, FileHandler
{
477 * Callback method invoked when a the user browsed into a different
478 * folder through the list of files
482 public void onBrowsedDownTo(OCFile folder
);
484 public void startDownloadForPreview(OCFile file
);
486 public void startMediaPreview(OCFile file
, int i
, boolean b
);
488 public void startImagePreview(OCFile file
);
490 public void startSyncFolderOperation(OCFile folder
);
493 * Getter for the current DataStorageManager in the container activity
495 public FileDataStorageManager
getStorageManager();
498 * Callback method invoked when a the 'transfer state' of a file
501 * This happens when a download or upload is started or ended for a
504 * This method is necessary by now to update the user interface of the
505 * double-pane layout in tablets because methods
506 * {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
507 * {@link FileUploaderBinder#isUploading(Account, OCFile)} won't provide
508 * the needed response before the method where this is called finishes.
510 * TODO Remove this when the transfer state of a file is kept in the
511 * database (other thing TODO)
513 * @param file OCFile which state changed.
514 * @param downloading Flag signaling if the file is now downloading.
515 * @param uploading Flag signaling if the file is now uploading.
517 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
);
522 public void onDismiss(EditNameDialog dialog
) {
523 if (dialog
.getResult()) {
524 String newFilename
= dialog
.getNewFilename();
525 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
526 RemoteOperation operation
= new RenameFileOperation(mTargetFile
,
527 AccountUtils
.getCurrentOwnCloudAccount(getActivity()), newFilename
,
528 mContainerActivity
.getStorageManager());
529 operation
.execute(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(),
530 mContainerActivity
, mHandler
, getSherlockActivity());
531 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
536 public void onConfirmation(String callerTag
) {
537 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
538 if (mContainerActivity
.getStorageManager().getFileById(mTargetFile
.getFileId()) != null
) {
539 RemoteOperation operation
= new RemoveFileOperation(mTargetFile
, true
,
540 mContainerActivity
.getStorageManager());
541 operation
.execute(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(),
542 mContainerActivity
, mHandler
, getSherlockActivity());
544 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
550 public void onNeutral(String callerTag
) {
551 mContainerActivity
.getStorageManager().removeFile(mTargetFile
, false
, true
); // TODO
560 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
564 public void onCancel(String callerTag
) {
565 Log_OC
.d(TAG
, "REMOVAL CANCELED");