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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.ui
.fragment
;
22 import java
.util
.ArrayList
;
23 import java
.util
.List
;
25 import com
.owncloud
.android
.AccountUtils
;
26 import com
.owncloud
.android
.Log_OC
;
27 import com
.owncloud
.android
.R
;
28 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
29 import com
.owncloud
.android
.datamodel
.OCFile
;
30 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
31 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
32 import com
.owncloud
.android
.network
.OwnCloudClientUtils
;
33 import com
.owncloud
.android
.operations
.OnRemoteOperationListener
;
34 import com
.owncloud
.android
.operations
.RemoteOperation
;
35 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
36 import com
.owncloud
.android
.operations
.RenameFileOperation
;
37 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
38 import com
.owncloud
.android
.ui
.FragmentListView
;
39 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
40 import com
.owncloud
.android
.ui
.activity
.TransferServiceGetter
;
41 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
42 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
43 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
44 import com
.owncloud
.android
.ui
.fragment
.ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
;
46 import eu
.alefzero
.webdav
.WebdavClient
;
47 import eu
.alefzero
.webdav
.WebdavUtils
;
49 import android
.accounts
.Account
;
50 import android
.app
.Activity
;
51 import android
.content
.ActivityNotFoundException
;
52 import android
.content
.Intent
;
53 import android
.net
.Uri
;
54 import android
.os
.Bundle
;
55 import android
.os
.Handler
;
56 import android
.support
.v4
.app
.DialogFragment
;
57 import android
.util
.Log
;
58 import android
.view
.ContextMenu
;
59 import android
.view
.MenuInflater
;
60 import android
.view
.MenuItem
;
61 import android
.view
.View
;
62 import android
.webkit
.MimeTypeMap
;
63 import android
.widget
.AdapterView
;
64 import android
.widget
.Toast
;
65 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
68 * A Fragment that lists all files and folders in a given path.
70 * @author Bartek Przybylski
73 public class OCFileListFragment
extends FragmentListView
implements EditNameDialogListener
, ConfirmationDialogFragmentListener
{
74 private static final String TAG
= "FileListFragment";
75 private static final String SAVED_LIST_POSITION
= "LIST_POSITION";
77 private OCFileListFragment
.ContainerActivity mContainerActivity
;
79 private OCFile mFile
= null
;
80 private FileListListAdapter mAdapter
;
82 private Handler mHandler
;
83 private OCFile mTargetFile
;
89 public void onAttach(Activity activity
) {
90 super.onAttach(activity
);
92 mContainerActivity
= (ContainerActivity
) activity
;
93 } catch (ClassCastException e
) {
94 throw new ClassCastException(activity
.toString() + " must implement " + OCFileListFragment
.ContainerActivity
.class.getSimpleName());
103 public void onActivityCreated(Bundle savedInstanceState
) {
104 Log_OC
.i(TAG
, "onActivityCreated() start");
106 super.onActivityCreated(savedInstanceState
);
107 mAdapter
= new FileListListAdapter(mContainerActivity
.getInitialDirectory(), mContainerActivity
.getStorageManager(), getActivity(), mContainerActivity
);
108 setListAdapter(mAdapter
);
110 if (savedInstanceState
!= null
) {
111 Log_OC
.i(TAG
, "savedInstanceState is not null");
112 int position
= savedInstanceState
.getInt(SAVED_LIST_POSITION
);
113 setReferencePosition(position
);
116 registerForContextMenu(getListView());
117 getListView().setOnCreateContextMenuListener(this);
119 mHandler
= new Handler();
121 Log_OC
.i(TAG
, "onActivityCreated() stop");
127 public void onSaveInstanceState(Bundle savedInstanceState
) {
128 Log_OC
.i(TAG
, "onSaveInstanceState() start");
130 savedInstanceState
.putInt(SAVED_LIST_POSITION
, getReferencePosition());
132 Log_OC
.i(TAG
, "onSaveInstanceState() stop");
137 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
138 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
140 /// Click on a directory
141 if (file
.getMimetype().equals("DIR")) {
142 // just local updates
145 // any other updates are let to the container Activity
146 mContainerActivity
.onDirectoryClick(file
);
148 } else { /// Click on a file
149 mContainerActivity
.onFileClick(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_see_details
);
179 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
) ||
180 mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
) ) {
181 toDisable
.add(R
.id
.action_rename_file
);
182 toDisable
.add(R
.id
.action_remove_file
);
187 // contextual menu for regular files
188 if (targetFile
.isDown()) {
189 toHide
.add(R
.id
.action_cancel_download
);
190 toHide
.add(R
.id
.action_cancel_upload
);
191 item
= menu
.findItem(R
.id
.action_download_file
);
193 item
.setTitle(R
.string
.filedetails_sync_file
);
196 toHide
.add(R
.id
.action_open_file_with
);
198 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
199 toHide
.add(R
.id
.action_download_file
);
200 toHide
.add(R
.id
.action_cancel_upload
);
201 toDisable
.add(R
.id
.action_open_file_with
);
202 toDisable
.add(R
.id
.action_rename_file
);
203 toDisable
.add(R
.id
.action_remove_file
);
205 } else if ( mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
206 toHide
.add(R
.id
.action_download_file
);
207 toHide
.add(R
.id
.action_cancel_download
);
208 toDisable
.add(R
.id
.action_open_file_with
);
209 toDisable
.add(R
.id
.action_rename_file
);
210 toDisable
.add(R
.id
.action_remove_file
);
213 toHide
.add(R
.id
.action_cancel_download
);
214 toHide
.add(R
.id
.action_cancel_upload
);
218 for (int i
: toHide
) {
219 item
= menu
.findItem(i
);
221 item
.setVisible(false
);
222 item
.setEnabled(false
);
226 for (int i
: toDisable
) {
227 item
= menu
.findItem(i
);
229 item
.setEnabled(false
);
239 public boolean onContextItemSelected (MenuItem item
) {
240 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
241 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
242 switch (item
.getItemId()) {
243 case R
.id
.action_rename_file
: {
244 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), mTargetFile
.getFileName(), this);
245 dialog
.show(getFragmentManager(), EditNameDialog
.TAG
);
248 case R
.id
.action_remove_file
: {
249 int messageStringId
= R
.string
.confirmation_remove_alert
;
250 int posBtnStringId
= R
.string
.confirmation_remove_remote
;
251 int neuBtnStringId
= -1;
252 if (mTargetFile
.isDirectory()) {
253 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
254 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
255 neuBtnStringId
= R
.string
.confirmation_remove_folder_local
;
256 } else if (mTargetFile
.isDown()) {
257 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
258 neuBtnStringId
= R
.string
.confirmation_remove_local
;
260 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
262 new String
[]{mTargetFile
.getFileName()},
265 R
.string
.common_cancel
);
266 confDialog
.setOnConfirmationListener(this);
267 confDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
270 case R
.id
.action_open_file_with
: {
271 String storagePath
= mTargetFile
.getStoragePath();
272 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
274 Intent i
= new Intent(Intent
.ACTION_VIEW
);
275 i
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), mTargetFile
.getMimetype());
276 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
279 } catch (Throwable t
) {
280 Log_OC
.e(TAG
, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mTargetFile
.getMimetype());
281 boolean toastIt
= true
;
282 String mimeType
= "";
284 Intent i
= new Intent(Intent
.ACTION_VIEW
);
285 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
286 if (mimeType
== null
|| !mimeType
.equals(mTargetFile
.getMimetype())) {
287 if (mimeType
!= null
) {
288 i
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), mimeType
);
291 i
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), "*/*");
293 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
298 } catch (IndexOutOfBoundsException e
) {
299 Log_OC
.e(TAG
, "Trying to find out MIME type of a file without extension: " + storagePath
);
301 } catch (ActivityNotFoundException e
) {
302 Log_OC
.e(TAG
, "No activity found to handle: " + storagePath
+ " with MIME type " + mimeType
+ " obtained from extension");
304 } catch (Throwable th
) {
305 Log_OC
.e(TAG
, "Unexpected problem when opening: " + storagePath
, th
);
309 Toast
.makeText(getActivity(), "There is no application to handle file " + mTargetFile
.getFileName(), Toast
.LENGTH_SHORT
).show();
316 case R
.id
.action_download_file
: {
317 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
318 RemoteOperation operation
= new SynchronizeFileOperation(mTargetFile
, null
, mContainerActivity
.getStorageManager(), account
, true
, false
, getSherlockActivity());
319 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(account
, getSherlockActivity().getApplicationContext());
320 operation
.execute(wc
, mContainerActivity
, mHandler
);
321 getSherlockActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
324 case R
.id
.action_cancel_download
: {
325 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
326 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
327 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, mTargetFile
)) {
328 downloaderBinder
.cancel(account
, mTargetFile
);
330 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
334 case R
.id
.action_cancel_upload
: {
335 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
336 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
337 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, mTargetFile
)) {
338 uploaderBinder
.cancel(account
, mTargetFile
);
340 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
344 case R
.id
.action_see_details
: {
345 ((FileFragment
.ContainerActivity
)getActivity()).showFragmentWithDetails(mTargetFile
);
349 return super.onContextItemSelected(item
);
355 * Call this, when the user presses the up button
357 public void onNavigateUp() {
358 OCFile parentDir
= null
;
361 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
362 parentDir
= storageManager
.getFileById(mFile
.getParentId());
365 listDirectory(parentDir
);
369 * Use this to query the {@link OCFile} that is currently
370 * being displayed by this fragment
371 * @return The currently viewed OCFile
373 public OCFile
getCurrentFile(){
378 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
380 public void listDirectory(){
385 * Lists the given directory on the view. When the input parameter is null,
386 * it will either refresh the last known directory, or list the root
387 * if there never was a directory.
389 * @param directory File to be listed
391 public void listDirectory(OCFile directory
) {
392 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
393 if (storageManager
!= null
) {
395 // Check input parameters for null
396 if(directory
== null
){
400 directory
= storageManager
.getFileByPath("/");
401 if (directory
== null
) return; // no files, wait for sync
406 // If that's not a directory -> List its parent
407 if(!directory
.isDirectory()){
408 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
409 directory
= storageManager
.getFileById(directory
.getParentId());
412 mAdapter
.swapDirectory(directory
, storageManager
);
413 if (mFile
== null
|| !mFile
.equals(directory
)) {
414 mList
.setSelectionFromTop(0, 0);
423 * Interface to implement by any Activity that includes some instance of FileListFragment
425 * @author David A. Velasco
427 public interface ContainerActivity
extends TransferServiceGetter
, OnRemoteOperationListener
{
430 * Callback method invoked when a directory is clicked by the user on the files list
434 public void onDirectoryClick(OCFile file
);
437 * Callback method invoked when a file (non directory) is clicked by the user on the files list
441 public void onFileClick(OCFile file
);
444 * Getter for the current DataStorageManager in the container activity
446 public DataStorageManager
getStorageManager();
450 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
452 * @return Directory to list firstly. Can be NULL.
454 public OCFile
getInitialDirectory();
458 * Callback method invoked when a the 'transfer state' of a file changes.
460 * This happens when a download or upload is started or ended for a file.
462 * This method is necessary by now to update the user interface of the double-pane layout in tablets
463 * because methods {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and {@link FileUploaderBinder#isUploading(Account, OCFile)}
464 * won't provide the needed response before the method where this is called finishes.
466 * TODO Remove this when the transfer state of a file is kept in the database (other thing TODO)
468 * @param file OCFile which state changed.
469 * @param downloading Flag signaling if the file is now downloading.
470 * @param uploading Flag signaling if the file is now uploading.
472 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
);
478 public void onDismiss(EditNameDialog dialog
) {
479 if (dialog
.getResult()) {
480 String newFilename
= dialog
.getNewFilename();
481 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
482 RemoteOperation operation
= new RenameFileOperation(mTargetFile
,
483 AccountUtils
.getCurrentOwnCloudAccount(getActivity()),
485 mContainerActivity
.getStorageManager());
486 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity().getApplicationContext());
487 operation
.execute(wc
, mContainerActivity
, mHandler
);
488 getActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
494 public void onConfirmation(String callerTag
) {
495 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
496 if (mContainerActivity
.getStorageManager().getFileById(mTargetFile
.getFileId()) != null
) {
497 RemoteOperation operation
= new RemoveFileOperation( mTargetFile
,
499 mContainerActivity
.getStorageManager());
500 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity().getApplicationContext());
501 operation
.execute(wc
, mContainerActivity
, mHandler
);
503 getActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
509 public void onNeutral(String callerTag
) {
511 if (mTargetFile
.isDirectory()) {
512 // TODO run in a secondary thread?
513 mContainerActivity
.getStorageManager().removeDirectory(mTargetFile
, false
, true
);
515 } else if (mTargetFile
.isDown() && (f
= new File(mTargetFile
.getStoragePath())).exists()) {
517 mTargetFile
.setStoragePath(null
);
518 mContainerActivity
.getStorageManager().saveFile(mTargetFile
);
521 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
525 public void onCancel(String callerTag
) {
526 Log_OC
.d(TAG
, "REMOVAL CANCELED");