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
.Log_OC
;
25 import com
.owncloud
.android
.R
;
26 import com
.owncloud
.android
.authentication
.AccountUtils
;
27 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
28 import com
.owncloud
.android
.datamodel
.OCFile
;
29 import com
.owncloud
.android
.files
.FileHandler
;
30 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
31 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
32 import com
.owncloud
.android
.oc_framework
.operations
.OnRemoteOperationListener
;
33 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperation
;
34 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
35 import com
.owncloud
.android
.operations
.RenameFileOperation
;
36 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
37 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
38 import com
.owncloud
.android
.ui
.activity
.TransferServiceGetter
;
39 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
40 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
41 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
42 import com
.owncloud
.android
.ui
.fragment
.ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
;
43 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
44 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
46 import android
.accounts
.Account
;
47 import android
.app
.Activity
;
48 import android
.os
.Bundle
;
49 import android
.os
.Handler
;
50 import android
.view
.ContextMenu
;
51 import android
.view
.MenuInflater
;
52 import android
.view
.MenuItem
;
53 import android
.view
.View
;
54 import android
.widget
.AdapterView
;
55 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
58 * A Fragment that lists all files and folders in a given path.
60 * @author Bartek Przybylski
63 public class OCFileListFragment
extends ExtendedListFragment
implements EditNameDialogListener
, ConfirmationDialogFragmentListener
{
65 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
67 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ? OCFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
68 private static final String EXTRA_FILE
= MY_PACKAGE
+ ".extra.FILE";
70 private OCFileListFragment
.ContainerActivity mContainerActivity
;
72 private OCFile mFile
= null
;
73 private FileListListAdapter mAdapter
;
75 private Handler mHandler
;
76 private OCFile mTargetFile
;
82 public void onAttach(Activity activity
) {
83 super.onAttach(activity
);
84 Log_OC
.e(TAG
, "onAttach");
86 mContainerActivity
= (ContainerActivity
) activity
;
87 } catch (ClassCastException e
) {
88 throw new ClassCastException(activity
.toString() + " must implement " + OCFileListFragment
.ContainerActivity
.class.getSimpleName());
97 public void onActivityCreated(Bundle savedInstanceState
) {
98 super.onActivityCreated(savedInstanceState
);
99 Log_OC
.e(TAG
, "onActivityCreated() start");
100 mAdapter
= new FileListListAdapter(getActivity(), mContainerActivity
);
101 if (savedInstanceState
!= null
) {
102 mFile
= savedInstanceState
.getParcelable(EXTRA_FILE
);
104 setListAdapter(mAdapter
);
106 registerForContextMenu(getListView());
107 getListView().setOnCreateContextMenuListener(this);
109 mHandler
= new Handler();
114 * Saves the current listed folder.
117 public void onSaveInstanceState (Bundle outState
) {
118 super.onSaveInstanceState(outState
);
119 outState
.putParcelable(EXTRA_FILE
, mFile
);
124 * Call this, when the user presses the up button.
126 * Tries to move up the current folder one level. If the parent folder was removed from the database,
127 * it continues browsing up until finding an existing folders.
129 * return Count of folder levels browsed up.
131 public int onBrowseUp() {
132 OCFile parentDir
= null
;
136 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
138 String parentPath
= null
;
139 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
140 parentPath
= new File(mFile
.getRemotePath()).getParent();
141 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
142 parentDir
= storageManager
.getFileByPath(parentPath
);
145 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
); // never returns null; keep the path in root folder
147 while (parentDir
== null
) {
148 parentPath
= new File(parentPath
).getParent();
149 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
150 parentDir
= storageManager
.getFileByPath(parentPath
);
152 } // exit is granted because storageManager.getFileByPath("/") never returns null
157 listDirectory(mFile
);
159 mContainerActivity
.startSyncFolderOperation(mFile
);
160 } // else - should never happen now
166 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
167 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
169 if (file
.isFolder()) {
170 // update state and view of this fragment
172 // then, notify parent activity to let it update its state and view, and other fragments
173 mContainerActivity
.onBrowsedDownTo(file
);
175 } else { /// Click on a file
176 if (PreviewImageFragment
.canBePreviewed(file
)) {
177 // preview image - it handles the download, if needed
178 mContainerActivity
.startImagePreview(file
);
180 } else if (file
.isDown()) {
181 if (PreviewMediaFragment
.canBePreviewed(file
)) {
183 mContainerActivity
.startMediaPreview(file
, 0, true
);
186 mContainerActivity
.openFile(file
);
190 // automatic download, preview on finish
191 mContainerActivity
.startDownloadForPreview(file
);
197 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
206 public void onCreateContextMenu (ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
207 super.onCreateContextMenu(menu
, v
, menuInfo
);
208 MenuInflater inflater
= getActivity().getMenuInflater();
209 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
210 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
211 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
212 List
<Integer
> toHide
= new ArrayList
<Integer
>();
213 List
<Integer
> toDisable
= new ArrayList
<Integer
>();
215 MenuItem item
= null
;
216 if (targetFile
.isFolder()) {
217 // contextual menu for folders
218 toHide
.add(R
.id
.action_open_file_with
);
219 toHide
.add(R
.id
.action_download_file
);
220 toHide
.add(R
.id
.action_cancel_download
);
221 toHide
.add(R
.id
.action_cancel_upload
);
222 toHide
.add(R
.id
.action_sync_file
);
223 toHide
.add(R
.id
.action_see_details
);
224 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
) ||
225 mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
) ) {
226 toDisable
.add(R
.id
.action_rename_file
);
227 toDisable
.add(R
.id
.action_remove_file
);
232 // contextual menu for regular files
234 // new design: 'download' and 'open with' won't be available anymore in context menu
235 toHide
.add(R
.id
.action_download_file
);
236 toHide
.add(R
.id
.action_open_file_with
);
238 if (targetFile
.isDown()) {
239 toHide
.add(R
.id
.action_cancel_download
);
240 toHide
.add(R
.id
.action_cancel_upload
);
243 toHide
.add(R
.id
.action_sync_file
);
245 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
246 toHide
.add(R
.id
.action_cancel_upload
);
247 toDisable
.add(R
.id
.action_rename_file
);
248 toDisable
.add(R
.id
.action_remove_file
);
250 } else if ( mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
251 toHide
.add(R
.id
.action_cancel_download
);
252 toDisable
.add(R
.id
.action_rename_file
);
253 toDisable
.add(R
.id
.action_remove_file
);
256 toHide
.add(R
.id
.action_cancel_download
);
257 toHide
.add(R
.id
.action_cancel_upload
);
261 for (int i
: toHide
) {
262 item
= menu
.findItem(i
);
264 item
.setVisible(false
);
265 item
.setEnabled(false
);
269 for (int i
: toDisable
) {
270 item
= menu
.findItem(i
);
272 item
.setEnabled(false
);
282 public boolean onContextItemSelected (MenuItem item
) {
283 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
284 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
285 switch (item
.getItemId()) {
286 case R
.id
.action_rename_file
: {
287 String fileName
= mTargetFile
.getFileName();
288 int extensionStart
= mTargetFile
.isFolder() ?
-1 : fileName
.lastIndexOf(".");
289 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: fileName
.length();
290 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), fileName
, 0, selectionEnd
, this);
291 dialog
.show(getFragmentManager(), EditNameDialog
.TAG
);
294 case R
.id
.action_remove_file
: {
295 int messageStringId
= R
.string
.confirmation_remove_alert
;
296 int posBtnStringId
= R
.string
.confirmation_remove_remote
;
297 int neuBtnStringId
= -1;
298 if (mTargetFile
.isFolder()) {
299 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
300 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
301 neuBtnStringId
= R
.string
.confirmation_remove_folder_local
;
302 } else if (mTargetFile
.isDown()) {
303 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
304 neuBtnStringId
= R
.string
.confirmation_remove_local
;
306 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
308 new String
[]{mTargetFile
.getFileName()},
311 R
.string
.common_cancel
);
312 confDialog
.setOnConfirmationListener(this);
313 confDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
316 case R
.id
.action_sync_file
: {
317 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
318 RemoteOperation operation
= new SynchronizeFileOperation(mTargetFile
, null
, mContainerActivity
.getStorageManager(), account
, true
, getSherlockActivity());
319 operation
.execute(account
, getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
320 ((FileDisplayActivity
) getSherlockActivity()).showLoadingDialog();
323 case R
.id
.action_cancel_download
: {
324 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
325 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
326 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, mTargetFile
)) {
327 downloaderBinder
.cancel(account
, mTargetFile
);
329 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
333 case R
.id
.action_cancel_upload
: {
334 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
335 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
336 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, mTargetFile
)) {
337 uploaderBinder
.cancel(account
, mTargetFile
);
339 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
343 case R
.id
.action_see_details
: {
344 ((FileFragment
.ContainerActivity
)getActivity()).showDetails(mTargetFile
);
348 return super.onContextItemSelected(item
);
354 * Use this to query the {@link OCFile} that is currently
355 * being displayed by this fragment
356 * @return The currently viewed OCFile
358 public OCFile
getCurrentFile(){
363 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
365 public void listDirectory(){
370 * Lists the given directory on the view. When the input parameter is null,
371 * it will either refresh the last known directory. list the root
372 * if there never was a directory.
374 * @param directory File to be listed
376 public void listDirectory(OCFile directory
) {
377 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
378 if (storageManager
!= null
) {
380 // Check input parameters for null
381 if(directory
== null
){
385 directory
= storageManager
.getFileByPath("/");
386 if (directory
== null
) return; // no files, wait for sync
391 // If that's not a directory -> List its parent
392 if(!directory
.isFolder()){
393 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
394 directory
= storageManager
.getFileById(directory
.getParentId());
397 mAdapter
.swapDirectory(directory
, storageManager
);
398 if (mFile
== null
|| !mFile
.equals(directory
)) {
399 mList
.setSelectionFromTop(0, 0);
408 * Interface to implement by any Activity that includes some instance of FileListFragment
410 * @author David A. Velasco
412 public interface ContainerActivity
extends TransferServiceGetter
, OnRemoteOperationListener
, FileHandler
{
415 * Callback method invoked when a the user browsed into a different folder through the list of files
419 public void onBrowsedDownTo(OCFile folder
);
421 public void startDownloadForPreview(OCFile file
);
423 public void startMediaPreview(OCFile file
, int i
, boolean b
);
425 public void startImagePreview(OCFile file
);
427 public void startSyncFolderOperation(OCFile folder
);
430 * Getter for the current DataStorageManager in the container activity
432 public FileDataStorageManager
getStorageManager();
436 * Callback method invoked when a the 'transfer state' of a file changes.
438 * This happens when a download or upload is started or ended for a file.
440 * This method is necessary by now to update the user interface of the double-pane layout in tablets
441 * because methods {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and {@link FileUploaderBinder#isUploading(Account, OCFile)}
442 * won't provide the needed response before the method where this is called finishes.
444 * TODO Remove this when the transfer state of a file is kept in the database (other thing TODO)
446 * @param file OCFile which state changed.
447 * @param downloading Flag signaling if the file is now downloading.
448 * @param uploading Flag signaling if the file is now uploading.
450 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
);
456 public void onDismiss(EditNameDialog dialog
) {
457 if (dialog
.getResult()) {
458 String newFilename
= dialog
.getNewFilename();
459 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
460 RemoteOperation operation
= new RenameFileOperation(mTargetFile
,
461 AccountUtils
.getCurrentOwnCloudAccount(getActivity()),
463 mContainerActivity
.getStorageManager());
464 operation
.execute(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
465 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
471 public void onConfirmation(String callerTag
) {
472 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
473 if (mContainerActivity
.getStorageManager().getFileById(mTargetFile
.getFileId()) != null
) {
474 RemoteOperation operation
= new RemoveFileOperation( mTargetFile
,
476 mContainerActivity
.getStorageManager());
477 operation
.execute(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
479 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
485 public void onNeutral(String callerTag
) {
486 mContainerActivity
.getStorageManager().removeFile(mTargetFile
, false
, true
); // TODO perform in background task / new thread
488 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
492 public void onCancel(String callerTag
) {
493 Log_OC
.d(TAG
, "REMOVAL CANCELED");