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
.AccountUtils
;
25 import com
.owncloud
.android
.Log_OC
;
26 import com
.owncloud
.android
.R
;
27 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
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
.operations
.OnRemoteOperationListener
;
33 import com
.owncloud
.android
.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 OCFileListFragment
.ContainerActivity mContainerActivity
;
69 private OCFile mFile
= null
;
70 private FileListListAdapter mAdapter
;
72 private Handler mHandler
;
73 private OCFile mTargetFile
;
79 public void onAttach(Activity activity
) {
80 super.onAttach(activity
);
81 Log_OC
.e(TAG
, "onAttach");
83 mContainerActivity
= (ContainerActivity
) activity
;
84 } catch (ClassCastException e
) {
85 throw new ClassCastException(activity
.toString() + " must implement " + OCFileListFragment
.ContainerActivity
.class.getSimpleName());
94 public void onActivityCreated(Bundle savedInstanceState
) {
95 super.onActivityCreated(savedInstanceState
);
96 Log_OC
.e(TAG
, "onActivityCreated() start");
97 mAdapter
= new FileListListAdapter(getActivity(), mContainerActivity
);
98 setListAdapter(mAdapter
);
100 registerForContextMenu(getListView());
101 getListView().setOnCreateContextMenuListener(this);
103 mHandler
= new Handler();
108 * Call this, when the user presses the up button
110 public void onBrowseUp() {
111 OCFile parentDir
= null
;
114 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
115 parentDir
= storageManager
.getFileById(mFile
.getParentId());
118 listDirectory(parentDir
);
122 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
123 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
125 if (file
.isDirectory()) {
126 // update state and view of this fragment
128 // then, notify parent activity to let it update its state and view, and other fragments
129 mContainerActivity
.onBrowsedDownTo(file
);
131 } else { /// Click on a file
132 if (PreviewImageFragment
.canBePreviewed(file
)) {
133 // preview image - it handles the download, if needed
134 mContainerActivity
.startImagePreview(file
);
136 } else if (file
.isDown()) {
137 if (PreviewMediaFragment
.canBePreviewed(file
)) {
139 mContainerActivity
.startMediaPreview(file
, 0, true
);
142 mContainerActivity
.openFile(file
);
146 // automatic download, preview on finish
147 mContainerActivity
.startDownloadForPreview(file
);
153 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
162 public void onCreateContextMenu (ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
163 super.onCreateContextMenu(menu
, v
, menuInfo
);
164 MenuInflater inflater
= getActivity().getMenuInflater();
165 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
166 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
167 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
168 List
<Integer
> toHide
= new ArrayList
<Integer
>();
169 List
<Integer
> toDisable
= new ArrayList
<Integer
>();
171 MenuItem item
= null
;
172 if (targetFile
.isDirectory()) {
173 // contextual menu for folders
174 toHide
.add(R
.id
.action_open_file_with
);
175 toHide
.add(R
.id
.action_download_file
);
176 toHide
.add(R
.id
.action_cancel_download
);
177 toHide
.add(R
.id
.action_cancel_upload
);
178 toHide
.add(R
.id
.action_sync_file
);
179 toHide
.add(R
.id
.action_see_details
);
180 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
) ||
181 mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
) ) {
182 toDisable
.add(R
.id
.action_rename_file
);
183 toDisable
.add(R
.id
.action_remove_file
);
188 // contextual menu for regular files
190 // new design: 'download' and 'open with' won't be available anymore in context menu
191 toHide
.add(R
.id
.action_download_file
);
192 toHide
.add(R
.id
.action_open_file_with
);
194 if (targetFile
.isDown()) {
195 toHide
.add(R
.id
.action_cancel_download
);
196 toHide
.add(R
.id
.action_cancel_upload
);
199 toHide
.add(R
.id
.action_sync_file
);
201 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
202 toHide
.add(R
.id
.action_cancel_upload
);
203 toDisable
.add(R
.id
.action_rename_file
);
204 toDisable
.add(R
.id
.action_remove_file
);
206 } else if ( mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
207 toHide
.add(R
.id
.action_cancel_download
);
208 toDisable
.add(R
.id
.action_rename_file
);
209 toDisable
.add(R
.id
.action_remove_file
);
212 toHide
.add(R
.id
.action_cancel_download
);
213 toHide
.add(R
.id
.action_cancel_upload
);
217 for (int i
: toHide
) {
218 item
= menu
.findItem(i
);
220 item
.setVisible(false
);
221 item
.setEnabled(false
);
225 for (int i
: toDisable
) {
226 item
= menu
.findItem(i
);
228 item
.setEnabled(false
);
238 public boolean onContextItemSelected (MenuItem item
) {
239 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
240 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
241 switch (item
.getItemId()) {
242 case R
.id
.action_rename_file
: {
243 String fileName
= mTargetFile
.getFileName();
244 int extensionStart
= mTargetFile
.isDirectory() ?
-1 : fileName
.lastIndexOf(".");
245 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: fileName
.length();
246 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), fileName
, 0, selectionEnd
, this);
247 dialog
.show(getFragmentManager(), EditNameDialog
.TAG
);
250 case R
.id
.action_remove_file
: {
251 int messageStringId
= R
.string
.confirmation_remove_alert
;
252 int posBtnStringId
= R
.string
.confirmation_remove_remote
;
253 int neuBtnStringId
= -1;
254 if (mTargetFile
.isDirectory()) {
255 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
256 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
257 neuBtnStringId
= R
.string
.confirmation_remove_folder_local
;
258 } else if (mTargetFile
.isDown()) {
259 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
260 neuBtnStringId
= R
.string
.confirmation_remove_local
;
262 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
264 new String
[]{mTargetFile
.getFileName()},
267 R
.string
.common_cancel
);
268 confDialog
.setOnConfirmationListener(this);
269 confDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
272 case R
.id
.action_sync_file
: {
273 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
274 RemoteOperation operation
= new SynchronizeFileOperation(mTargetFile
, null
, mContainerActivity
.getStorageManager(), account
, true
, false
, getSherlockActivity());
275 operation
.execute(account
, getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
276 getSherlockActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
279 case R
.id
.action_cancel_download
: {
280 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
281 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
282 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, mTargetFile
)) {
283 downloaderBinder
.cancel(account
, mTargetFile
);
285 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
289 case R
.id
.action_cancel_upload
: {
290 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
291 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
292 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, mTargetFile
)) {
293 uploaderBinder
.cancel(account
, mTargetFile
);
295 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
299 case R
.id
.action_see_details
: {
300 ((FileFragment
.ContainerActivity
)getActivity()).showDetails(mTargetFile
);
304 return super.onContextItemSelected(item
);
310 * Use this to query the {@link OCFile} that is currently
311 * being displayed by this fragment
312 * @return The currently viewed OCFile
314 public OCFile
getCurrentFile(){
319 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
321 public void listDirectory(){
326 * Lists the given directory on the view. When the input parameter is null,
327 * it will either refresh the last known directory. list the root
328 * if there never was a directory.
330 * @param directory File to be listed
332 public void listDirectory(OCFile directory
) {
333 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
334 if (storageManager
!= null
) {
336 // Check input parameters for null
337 if(directory
== null
){
341 directory
= storageManager
.getFileByPath("/");
342 if (directory
== null
) return; // no files, wait for sync
347 // If that's not a directory -> List its parent
348 if(!directory
.isDirectory()){
349 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
350 directory
= storageManager
.getFileById(directory
.getParentId());
353 mAdapter
.swapDirectory(directory
, storageManager
);
354 if (mFile
== null
|| !mFile
.equals(directory
)) {
355 mList
.setSelectionFromTop(0, 0);
364 * Interface to implement by any Activity that includes some instance of FileListFragment
366 * @author David A. Velasco
368 public interface ContainerActivity
extends TransferServiceGetter
, OnRemoteOperationListener
, FileHandler
{
371 * Callback method invoked when a the user browsed into a different folder through the list of files
375 public void onBrowsedDownTo(OCFile folder
);
377 public void startDownloadForPreview(OCFile file
);
379 public void startMediaPreview(OCFile file
, int i
, boolean b
);
381 public void startImagePreview(OCFile file
);
384 * Getter for the current DataStorageManager in the container activity
386 public DataStorageManager
getStorageManager();
390 * Callback method invoked when a the 'transfer state' of a file changes.
392 * This happens when a download or upload is started or ended for a file.
394 * This method is necessary by now to update the user interface of the double-pane layout in tablets
395 * because methods {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and {@link FileUploaderBinder#isUploading(Account, OCFile)}
396 * won't provide the needed response before the method where this is called finishes.
398 * TODO Remove this when the transfer state of a file is kept in the database (other thing TODO)
400 * @param file OCFile which state changed.
401 * @param downloading Flag signaling if the file is now downloading.
402 * @param uploading Flag signaling if the file is now uploading.
404 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
);
410 public void onDismiss(EditNameDialog dialog
) {
411 if (dialog
.getResult()) {
412 String newFilename
= dialog
.getNewFilename();
413 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
414 RemoteOperation operation
= new RenameFileOperation(mTargetFile
,
415 AccountUtils
.getCurrentOwnCloudAccount(getActivity()),
417 mContainerActivity
.getStorageManager());
418 operation
.execute(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
419 getActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
425 public void onConfirmation(String callerTag
) {
426 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
427 if (mContainerActivity
.getStorageManager().getFileById(mTargetFile
.getFileId()) != null
) {
428 RemoteOperation operation
= new RemoveFileOperation( mTargetFile
,
430 mContainerActivity
.getStorageManager());
431 operation
.execute(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
433 getActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
439 public void onNeutral(String callerTag
) {
441 if (mTargetFile
.isDirectory()) {
442 // TODO run in a secondary thread?
443 mContainerActivity
.getStorageManager().removeDirectory(mTargetFile
, false
, true
);
445 } else if (mTargetFile
.isDown() && (f
= new File(mTargetFile
.getStoragePath())).exists()) {
447 mTargetFile
.setStoragePath(null
);
448 mContainerActivity
.getStorageManager().saveFile(mTargetFile
);
451 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
455 public void onCancel(String callerTag
) {
456 Log_OC
.d(TAG
, "REMOVAL CANCELED");