1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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
.R
;
26 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
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
.network
.OwnCloudClientUtils
;
31 import com
.owncloud
.android
.operations
.OnRemoteOperationListener
;
32 import com
.owncloud
.android
.operations
.RemoteOperation
;
33 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
34 import com
.owncloud
.android
.operations
.RenameFileOperation
;
35 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
36 import com
.owncloud
.android
.ui
.FragmentListView
;
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
;
44 import eu
.alefzero
.webdav
.WebdavClient
;
45 import eu
.alefzero
.webdav
.WebdavUtils
;
47 import android
.accounts
.Account
;
48 import android
.app
.Activity
;
49 import android
.content
.ActivityNotFoundException
;
50 import android
.content
.Intent
;
51 import android
.net
.Uri
;
52 import android
.os
.Bundle
;
53 import android
.os
.Handler
;
54 import android
.support
.v4
.app
.DialogFragment
;
55 import android
.util
.Log
;
56 import android
.view
.ContextMenu
;
57 import android
.view
.MenuInflater
;
58 import android
.view
.MenuItem
;
59 import android
.view
.View
;
60 import android
.webkit
.MimeTypeMap
;
61 import android
.widget
.AdapterView
;
62 import android
.widget
.Toast
;
63 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
66 * A Fragment that lists all files and folders in a given path.
68 * @author Bartek Przybylski
71 public class OCFileListFragment
extends FragmentListView
implements EditNameDialogListener
, ConfirmationDialogFragmentListener
{
72 private static final String TAG
= "FileListFragment";
73 private static final String SAVED_LIST_POSITION
= "LIST_POSITION";
75 private OCFileListFragment
.ContainerActivity mContainerActivity
;
77 private OCFile mFile
= null
;
78 private FileListListAdapter mAdapter
;
80 private Handler mHandler
;
81 private OCFile mTargetFile
;
87 public void onAttach(Activity activity
) {
88 super.onAttach(activity
);
90 mContainerActivity
= (ContainerActivity
) activity
;
91 } catch (ClassCastException e
) {
92 throw new ClassCastException(activity
.toString() + " must implement " + OCFileListFragment
.ContainerActivity
.class.getSimpleName());
101 public void onActivityCreated(Bundle savedInstanceState
) {
102 Log
.i(TAG
, "onActivityCreated() start");
104 super.onActivityCreated(savedInstanceState
);
105 mAdapter
= new FileListListAdapter(mContainerActivity
.getInitialDirectory(), mContainerActivity
.getStorageManager(), getActivity(), mContainerActivity
);
106 setListAdapter(mAdapter
);
108 if (savedInstanceState
!= null
) {
109 Log
.i(TAG
, "savedInstanceState is not null");
110 int position
= savedInstanceState
.getInt(SAVED_LIST_POSITION
);
111 setReferencePosition(position
);
114 registerForContextMenu(getListView());
115 getListView().setOnCreateContextMenuListener(this);
117 mHandler
= new Handler();
119 Log
.i(TAG
, "onActivityCreated() stop");
125 public void onSaveInstanceState(Bundle savedInstanceState
) {
126 Log
.i(TAG
, "onSaveInstanceState() start");
128 savedInstanceState
.putInt(SAVED_LIST_POSITION
, getReferencePosition());
130 Log
.i(TAG
, "onSaveInstanceState() stop");
135 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
136 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
138 /// Click on a directory
139 if (file
.getMimetype().equals("DIR")) {
140 // just local updates
143 // any other updates are let to the container Activity
144 mContainerActivity
.onDirectoryClick(file
);
146 } else { /// Click on a file
147 mContainerActivity
.onFileClick(file
);
151 Log
.d(TAG
, "Null object in ListAdapter!!");
160 public void onCreateContextMenu (ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
161 super.onCreateContextMenu(menu
, v
, menuInfo
);
162 MenuInflater inflater
= getActivity().getMenuInflater();
163 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
164 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
165 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
166 List
<Integer
> toHide
= new ArrayList
<Integer
>();
167 List
<Integer
> toDisable
= new ArrayList
<Integer
>();
169 MenuItem item
= null
;
170 if (targetFile
.isDirectory()) {
171 // contextual menu for folders
172 toHide
.add(R
.id
.action_open_file_with
);
173 toHide
.add(R
.id
.action_download_file
);
174 toHide
.add(R
.id
.action_cancel_download
);
175 toHide
.add(R
.id
.action_cancel_upload
);
176 toHide
.add(R
.id
.action_see_details
);
177 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
) ||
178 mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
) ) {
179 toDisable
.add(R
.id
.action_rename_file
);
180 toDisable
.add(R
.id
.action_remove_file
);
185 // contextual menu for regular files
186 if (targetFile
.isDown()) {
187 toHide
.add(R
.id
.action_cancel_download
);
188 toHide
.add(R
.id
.action_cancel_upload
);
189 item
= menu
.findItem(R
.id
.action_download_file
);
191 item
.setTitle(R
.string
.filedetails_sync_file
);
194 toHide
.add(R
.id
.action_open_file_with
);
196 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
197 toHide
.add(R
.id
.action_download_file
);
198 toHide
.add(R
.id
.action_cancel_upload
);
199 toDisable
.add(R
.id
.action_open_file_with
);
200 toDisable
.add(R
.id
.action_rename_file
);
201 toDisable
.add(R
.id
.action_remove_file
);
203 } else if ( mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
204 toHide
.add(R
.id
.action_download_file
);
205 toHide
.add(R
.id
.action_cancel_download
);
206 toDisable
.add(R
.id
.action_open_file_with
);
207 toDisable
.add(R
.id
.action_rename_file
);
208 toDisable
.add(R
.id
.action_remove_file
);
211 toHide
.add(R
.id
.action_cancel_download
);
212 toHide
.add(R
.id
.action_cancel_upload
);
216 for (int i
: toHide
) {
217 item
= menu
.findItem(i
);
219 item
.setVisible(false
);
220 item
.setEnabled(false
);
224 for (int i
: toDisable
) {
225 item
= menu
.findItem(i
);
227 item
.setEnabled(false
);
237 public boolean onContextItemSelected (MenuItem item
) {
238 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
239 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
240 switch (item
.getItemId()) {
241 case R
.id
.action_rename_file
: {
242 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), mTargetFile
.getFileName(), this);
243 dialog
.show(getFragmentManager(), EditNameDialog
.TAG
);
246 case R
.id
.action_remove_file
: {
247 int messageStringId
= R
.string
.confirmation_remove_alert
;
248 int posBtnStringId
= R
.string
.confirmation_remove_remote
;
249 int neuBtnStringId
= -1;
250 if (mTargetFile
.isDirectory()) {
251 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
252 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
253 neuBtnStringId
= R
.string
.confirmation_remove_folder_local
;
254 } else if (mTargetFile
.isDown()) {
255 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
256 neuBtnStringId
= R
.string
.confirmation_remove_local
;
258 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
260 new String
[]{mTargetFile
.getFileName()},
263 R
.string
.common_cancel
);
264 confDialog
.setOnConfirmationListener(this);
265 confDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
268 case R
.id
.action_open_file_with
: {
269 String storagePath
= mTargetFile
.getStoragePath();
270 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
272 Intent i
= new Intent(Intent
.ACTION_VIEW
);
273 i
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), mTargetFile
.getMimetype());
274 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
277 } catch (Throwable t
) {
278 Log
.e(TAG
, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mTargetFile
.getMimetype());
279 boolean toastIt
= true
;
280 String mimeType
= "";
282 Intent i
= new Intent(Intent
.ACTION_VIEW
);
283 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
284 if (mimeType
== null
|| !mimeType
.equals(mTargetFile
.getMimetype())) {
285 if (mimeType
!= null
) {
286 i
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), mimeType
);
289 i
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), "*/*");
291 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
296 } catch (IndexOutOfBoundsException e
) {
297 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + storagePath
);
299 } catch (ActivityNotFoundException e
) {
300 Log
.e(TAG
, "No activity found to handle: " + storagePath
+ " with MIME type " + mimeType
+ " obtained from extension");
302 } catch (Throwable th
) {
303 Log
.e(TAG
, "Unexpected problem when opening: " + storagePath
, th
);
307 Toast
.makeText(getActivity(), "There is no application to handle file " + mTargetFile
.getFileName(), Toast
.LENGTH_SHORT
).show();
314 case R
.id
.action_download_file
: {
315 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
316 RemoteOperation operation
= new SynchronizeFileOperation(mTargetFile
, null
, mContainerActivity
.getStorageManager(), account
, true
, false
, getSherlockActivity());
317 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(account
, getSherlockActivity().getApplicationContext());
318 operation
.execute(wc
, mContainerActivity
, mHandler
);
319 getSherlockActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
322 case R
.id
.action_cancel_download
: {
323 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
324 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
325 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, mTargetFile
)) {
326 downloaderBinder
.cancel(account
, mTargetFile
);
328 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
332 case R
.id
.action_cancel_upload
: {
333 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
334 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
335 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, mTargetFile
)) {
336 uploaderBinder
.cancel(account
, mTargetFile
);
338 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
342 case R
.id
.action_see_details
: {
343 ((FileFragment
.ContainerActivity
)getActivity()).showFragmentWithDetails(mTargetFile
);
347 return super.onContextItemSelected(item
);
353 * Call this, when the user presses the up button
355 public void onNavigateUp() {
356 OCFile parentDir
= null
;
359 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
360 parentDir
= storageManager
.getFileById(mFile
.getParentId());
363 listDirectory(parentDir
);
367 * Use this to query the {@link OCFile} that is currently
368 * being displayed by this fragment
369 * @return The currently viewed OCFile
371 public OCFile
getCurrentFile(){
376 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
378 public void listDirectory(){
383 * Lists the given directory on the view. When the input parameter is null,
384 * it will either refresh the last known directory, or list the root
385 * if there never was a directory.
387 * @param directory File to be listed
389 public void listDirectory(OCFile directory
) {
390 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
391 if (storageManager
!= null
) {
393 // Check input parameters for null
394 if(directory
== null
){
398 directory
= storageManager
.getFileByPath("/");
399 if (directory
== null
) return; // no files, wait for sync
404 // If that's not a directory -> List its parent
405 if(!directory
.isDirectory()){
406 Log
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
407 directory
= storageManager
.getFileById(directory
.getParentId());
410 mAdapter
.swapDirectory(directory
, storageManager
);
411 if (mFile
== null
|| !mFile
.equals(directory
)) {
412 mList
.setSelectionFromTop(0, 0);
421 * Interface to implement by any Activity that includes some instance of FileListFragment
423 * @author David A. Velasco
425 public interface ContainerActivity
extends TransferServiceGetter
, OnRemoteOperationListener
{
428 * Callback method invoked when a directory is clicked by the user on the files list
432 public void onDirectoryClick(OCFile file
);
435 * Callback method invoked when a file (non directory) is clicked by the user on the files list
439 public void onFileClick(OCFile file
);
442 * Getter for the current DataStorageManager in the container activity
444 public DataStorageManager
getStorageManager();
448 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
450 * @return Directory to list firstly. Can be NULL.
452 public OCFile
getInitialDirectory();
456 * Callback method invoked when a the 'transfer state' of a file changes.
458 * This happens when a download or upload is started or ended for a file.
460 * This method is necessary by now to update the user interface of the double-pane layout in tablets
461 * because methods {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and {@link FileUploaderBinder#isUploading(Account, OCFile)}
462 * won't provide the needed response before the method where this is called finishes.
464 * TODO Remove this when the transfer state of a file is kept in the database (other thing TODO)
466 * @param file OCFile which state changed.
467 * @param downloading Flag signaling if the file is now downloading.
468 * @param uploading Flag signaling if the file is now uploading.
470 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
);
476 public void onDismiss(EditNameDialog dialog
) {
477 if (dialog
.getResult()) {
478 String newFilename
= dialog
.getNewFilename();
479 Log
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
480 RemoteOperation operation
= new RenameFileOperation(mTargetFile
,
481 AccountUtils
.getCurrentOwnCloudAccount(getActivity()),
483 mContainerActivity
.getStorageManager());
484 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity().getApplicationContext());
485 operation
.execute(wc
, mContainerActivity
, mHandler
);
486 getActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
492 public void onConfirmation(String callerTag
) {
493 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
494 if (mContainerActivity
.getStorageManager().getFileById(mTargetFile
.getFileId()) != null
) {
495 RemoteOperation operation
= new RemoveFileOperation( mTargetFile
,
497 mContainerActivity
.getStorageManager());
498 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity().getApplicationContext());
499 operation
.execute(wc
, mContainerActivity
, mHandler
);
501 getActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
507 public void onNeutral(String callerTag
) {
509 if (mTargetFile
.isDirectory()) {
510 // TODO run in a secondary thread?
511 mContainerActivity
.getStorageManager().removeDirectory(mTargetFile
, false
, true
);
513 } else if (mTargetFile
.isDown() && (f
= new File(mTargetFile
.getStoragePath())).exists()) {
515 mTargetFile
.setStoragePath(null
);
516 mContainerActivity
.getStorageManager().saveFile(mTargetFile
);
519 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
523 public void onCancel(String callerTag
) {
524 Log
.d(TAG
, "REMOVAL CANCELED");