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
.R
;
27 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
28 import com
.owncloud
.android
.datamodel
.OCFile
;
29 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
30 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
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
.WebdavUtils
;
46 import android
.accounts
.Account
;
47 import android
.app
.Activity
;
48 import android
.content
.ActivityNotFoundException
;
49 import android
.content
.Intent
;
50 import android
.net
.Uri
;
51 import android
.os
.Bundle
;
52 import android
.os
.Handler
;
53 import android
.support
.v4
.app
.DialogFragment
;
54 import android
.util
.Log
;
55 import android
.view
.ContextMenu
;
56 import android
.view
.MenuInflater
;
57 import android
.view
.MenuItem
;
58 import android
.view
.View
;
59 import android
.webkit
.MimeTypeMap
;
60 import android
.widget
.AdapterView
;
61 import android
.widget
.Toast
;
62 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
65 * A Fragment that lists all files and folders in a given path.
67 * @author Bartek Przybylski
70 public class OCFileListFragment
extends FragmentListView
implements EditNameDialogListener
, ConfirmationDialogFragmentListener
{
71 private static final String TAG
= "FileListFragment";
72 private static final String SAVED_LIST_POSITION
= "LIST_POSITION";
74 private OCFileListFragment
.ContainerActivity mContainerActivity
;
76 private OCFile mFile
= null
;
77 private FileListListAdapter mAdapter
;
79 private Handler mHandler
;
80 private OCFile mTargetFile
;
82 private DialogFragment mCurrentDialog
;
88 public void onAttach(Activity activity
) {
89 super.onAttach(activity
);
91 mContainerActivity
= (ContainerActivity
) activity
;
92 } catch (ClassCastException e
) {
93 throw new ClassCastException(activity
.toString() + " must implement " + OCFileListFragment
.ContainerActivity
.class.getSimpleName());
102 public void onActivityCreated(Bundle savedInstanceState
) {
103 Log
.i(TAG
, "onActivityCreated() start");
105 super.onActivityCreated(savedInstanceState
);
106 mAdapter
= new FileListListAdapter(mContainerActivity
.getInitialDirectory(), mContainerActivity
.getStorageManager(), getActivity(), mContainerActivity
);
107 setListAdapter(mAdapter
);
109 if (savedInstanceState
!= null
) {
110 Log
.i(TAG
, "savedInstanceState is not null");
111 int position
= savedInstanceState
.getInt(SAVED_LIST_POSITION
);
112 setReferencePosition(position
);
115 registerForContextMenu(getListView());
116 getListView().setOnCreateContextMenuListener(this);
118 mHandler
= new Handler();
120 Log
.i(TAG
, "onActivityCreated() stop");
126 public void onSaveInstanceState(Bundle savedInstanceState
) {
127 Log
.i(TAG
, "onSaveInstanceState() start");
129 savedInstanceState
.putInt(SAVED_LIST_POSITION
, getReferencePosition());
131 Log
.i(TAG
, "onSaveInstanceState() stop");
136 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
137 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
139 /// Click on a directory
140 if (file
.getMimetype().equals("DIR")) {
141 // just local updates
144 // any other updates are let to the container Activity
145 mContainerActivity
.onDirectoryClick(file
);
147 } else { /// Click on a file
148 mContainerActivity
.onFileClick(file
);
152 Log
.d(TAG
, "Null object in ListAdapter!!");
161 public void onCreateContextMenu (ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
162 super.onCreateContextMenu(menu
, v
, menuInfo
);
163 MenuInflater inflater
= getActivity().getMenuInflater();
164 inflater
.inflate(R
.menu
.file_context_menu
, menu
);
165 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
166 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
167 List
<Integer
> toHide
= new ArrayList
<Integer
>();
168 List
<Integer
> toDisable
= new ArrayList
<Integer
>();
170 MenuItem item
= null
;
171 if (targetFile
.isDirectory()) {
172 // contextual menu for folders
173 toHide
.add(R
.id
.open_file_item
);
174 toHide
.add(R
.id
.download_file_item
);
175 toHide
.add(R
.id
.cancel_download_item
);
176 toHide
.add(R
.id
.cancel_upload_item
);
177 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
) ||
178 mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
) ) {
179 toDisable
.add(R
.id
.rename_file_item
);
180 toDisable
.add(R
.id
.remove_file_item
);
185 // contextual menu for regular files
186 if (targetFile
.isDown()) {
187 toHide
.add(R
.id
.cancel_download_item
);
188 toHide
.add(R
.id
.cancel_upload_item
);
189 item
= menu
.findItem(R
.id
.download_file_item
);
191 item
.setTitle(R
.string
.filedetails_sync_file
);
194 toHide
.add(R
.id
.open_file_item
);
196 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
197 toHide
.add(R
.id
.download_file_item
);
198 toHide
.add(R
.id
.cancel_upload_item
);
199 toDisable
.add(R
.id
.open_file_item
);
200 toDisable
.add(R
.id
.rename_file_item
);
201 toDisable
.add(R
.id
.remove_file_item
);
203 } else if ( mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
204 toHide
.add(R
.id
.download_file_item
);
205 toHide
.add(R
.id
.cancel_download_item
);
206 toDisable
.add(R
.id
.open_file_item
);
207 toDisable
.add(R
.id
.rename_file_item
);
208 toDisable
.add(R
.id
.remove_file_item
);
211 toHide
.add(R
.id
.cancel_download_item
);
212 toHide
.add(R
.id
.cancel_upload_item
);
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
.rename_file_item
: {
242 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), mTargetFile
.getFileName(), this);
243 dialog
.show(getFragmentManager(), EditNameDialog
.TAG
);
246 case R
.id
.remove_file_item
: {
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 mCurrentDialog
= confDialog
;
266 mCurrentDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
269 case R
.id
.open_file_item
: {
270 String storagePath
= mTargetFile
.getStoragePath();
271 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
273 Intent i
= new Intent(Intent
.ACTION_VIEW
);
274 i
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), mTargetFile
.getMimetype());
275 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
278 } catch (Throwable t
) {
279 Log
.e(TAG
, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mTargetFile
.getMimetype());
280 boolean toastIt
= true
;
281 String mimeType
= "";
283 Intent i
= new Intent(Intent
.ACTION_VIEW
);
284 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
285 if (mimeType
== null
|| !mimeType
.equals(mTargetFile
.getMimetype())) {
286 if (mimeType
!= null
) {
287 i
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), mimeType
);
290 i
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), "*/*");
292 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
297 } catch (IndexOutOfBoundsException e
) {
298 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + storagePath
);
300 } catch (ActivityNotFoundException e
) {
301 Log
.e(TAG
, "No activity found to handle: " + storagePath
+ " with MIME type " + mimeType
+ " obtained from extension");
303 } catch (Throwable th
) {
304 Log
.e(TAG
, "Unexpected problem when opening: " + storagePath
, th
);
308 Toast
.makeText(getActivity(), "There is no application to handle file " + mTargetFile
.getFileName(), Toast
.LENGTH_SHORT
).show();
315 case R
.id
.download_file_item
: {
316 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
317 RemoteOperation operation
= new SynchronizeFileOperation(mTargetFile
, null
, mContainerActivity
.getStorageManager(), account
, true
, false
, getSherlockActivity());
318 operation
.execute(account
, getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
319 getSherlockActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
322 case R
.id
.cancel_download_item
: {
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
.cancel_upload_item
: {
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
);
343 return super.onContextItemSelected(item
);
349 * Call this, when the user presses the up button
351 public void onNavigateUp() {
352 OCFile parentDir
= null
;
355 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
356 parentDir
= storageManager
.getFileById(mFile
.getParentId());
359 listDirectory(parentDir
);
363 * Use this to query the {@link OCFile} that is currently
364 * being displayed by this fragment
365 * @return The currently viewed OCFile
367 public OCFile
getCurrentFile(){
372 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
374 public void listDirectory(){
379 * Lists the given directory on the view. When the input parameter is null,
380 * it will either refresh the last known directory, or list the root
381 * if there never was a directory.
383 * @param directory File to be listed
385 public void listDirectory(OCFile directory
) {
386 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
387 if (storageManager
!= null
) {
389 // Check input parameters for null
390 if(directory
== null
){
394 directory
= storageManager
.getFileByPath("/");
395 if (directory
== null
) return; // no files, wait for sync
400 // If that's not a directory -> List its parent
401 if(!directory
.isDirectory()){
402 Log
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
403 directory
= storageManager
.getFileById(directory
.getParentId());
406 mAdapter
.swapDirectory(directory
, storageManager
);
407 if (mFile
== null
|| !mFile
.equals(directory
)) {
408 mList
.setSelectionFromTop(0, 0);
417 * Interface to implement by any Activity that includes some instance of FileListFragment
419 * @author David A. Velasco
421 public interface ContainerActivity
extends TransferServiceGetter
, OnRemoteOperationListener
{
424 * Callback method invoked when a directory is clicked by the user on the files list
428 public void onDirectoryClick(OCFile file
);
431 * Callback method invoked when a file (non directory) is clicked by the user on the files list
435 public void onFileClick(OCFile file
);
438 * Getter for the current DataStorageManager in the container activity
440 public DataStorageManager
getStorageManager();
444 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
446 * @return Directory to list firstly. Can be NULL.
448 public OCFile
getInitialDirectory();
452 * Callback method invoked when a the 'transfer state' of a file changes.
454 * This happens when a download or upload is started or ended for a file.
456 * This method is necessary by now to update the user interface of the double-pane layout in tablets
457 * because methods {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and {@link FileUploaderBinder#isUploading(Account, OCFile)}
458 * won't provide the needed response before the method where this is called finishes.
460 * TODO Remove this when the transfer state of a file is kept in the database (other thing TODO)
462 * @param file OCFile which state changed.
463 * @param downloading Flag signaling if the file is now downloading.
464 * @param uploading Flag signaling if the file is now uploading.
466 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
);
472 public void onDismiss(EditNameDialog dialog
) {
473 if (dialog
.getResult()) {
474 String newFilename
= dialog
.getNewFilename();
475 Log
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
476 RemoteOperation operation
= new RenameFileOperation(mTargetFile
,
477 AccountUtils
.getCurrentOwnCloudAccount(getActivity()),
479 mContainerActivity
.getStorageManager());
480 operation
.execute(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
481 getActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
487 public void onConfirmation(String callerTag
) {
488 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
489 if (mContainerActivity
.getStorageManager().getFileById(mTargetFile
.getFileId()) != null
) {
490 RemoteOperation operation
= new RemoveFileOperation( mTargetFile
,
492 mContainerActivity
.getStorageManager());
493 operation
.execute(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity
, mHandler
, getSherlockActivity());
495 getActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
497 if (mCurrentDialog
!= null
) {
498 mCurrentDialog
.dismiss();
499 mCurrentDialog
= null
;
505 public void onNeutral(String callerTag
) {
507 if (mTargetFile
.isDirectory()) {
508 // TODO run in a secondary thread?
509 mContainerActivity
.getStorageManager().removeDirectory(mTargetFile
, false
, true
);
511 } else if (mTargetFile
.isDown() && (f
= new File(mTargetFile
.getStoragePath())).exists()) {
513 mTargetFile
.setStoragePath(null
);
514 mContainerActivity
.getStorageManager().saveFile(mTargetFile
);
516 if (mCurrentDialog
!= null
) {
517 mCurrentDialog
.dismiss();
518 mCurrentDialog
= null
;
521 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
525 public void onCancel(String callerTag
) {
526 Log
.d(TAG
, "REMOVAL CANCELED");
527 if (mCurrentDialog
!= null
) {
528 mCurrentDialog
.dismiss();
529 mCurrentDialog
= null
;