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
;
83 private DialogFragment mCurrentDialog
;
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
.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
.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
.i(TAG
, "onActivityCreated() stop");
127 public void onSaveInstanceState(Bundle savedInstanceState
) {
128 Log
.i(TAG
, "onSaveInstanceState() start");
130 savedInstanceState
.putInt(SAVED_LIST_POSITION
, getReferencePosition());
132 Log
.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
.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_context_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
.open_file_item
);
175 toHide
.add(R
.id
.download_file_item
);
176 toHide
.add(R
.id
.cancel_download_item
);
177 toHide
.add(R
.id
.cancel_upload_item
);
178 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
) ||
179 mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
) ) {
180 toDisable
.add(R
.id
.rename_file_item
);
181 toDisable
.add(R
.id
.remove_file_item
);
186 // contextual menu for regular files
187 if (targetFile
.isDown()) {
188 toHide
.add(R
.id
.cancel_download_item
);
189 toHide
.add(R
.id
.cancel_upload_item
);
190 item
= menu
.findItem(R
.id
.download_file_item
);
192 item
.setTitle(R
.string
.filedetails_sync_file
);
195 toHide
.add(R
.id
.open_file_item
);
197 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
198 toHide
.add(R
.id
.download_file_item
);
199 toHide
.add(R
.id
.cancel_upload_item
);
200 toDisable
.add(R
.id
.open_file_item
);
201 toDisable
.add(R
.id
.rename_file_item
);
202 toDisable
.add(R
.id
.remove_file_item
);
204 } else if ( mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
205 toHide
.add(R
.id
.download_file_item
);
206 toHide
.add(R
.id
.cancel_download_item
);
207 toDisable
.add(R
.id
.open_file_item
);
208 toDisable
.add(R
.id
.rename_file_item
);
209 toDisable
.add(R
.id
.remove_file_item
);
212 toHide
.add(R
.id
.cancel_download_item
);
213 toHide
.add(R
.id
.cancel_upload_item
);
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
.rename_file_item
: {
243 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), mTargetFile
.getFileName(), this);
244 dialog
.show(getFragmentManager(), EditNameDialog
.TAG
);
247 case R
.id
.remove_file_item
: {
248 int messageStringId
= R
.string
.confirmation_remove_alert
;
249 int posBtnStringId
= R
.string
.confirmation_remove_remote
;
250 int neuBtnStringId
= -1;
251 if (mTargetFile
.isDirectory()) {
252 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
253 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
254 neuBtnStringId
= R
.string
.confirmation_remove_folder_local
;
255 } else if (mTargetFile
.isDown()) {
256 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
257 neuBtnStringId
= R
.string
.confirmation_remove_local
;
259 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
261 new String
[]{mTargetFile
.getFileName()},
264 R
.string
.common_cancel
);
265 confDialog
.setOnConfirmationListener(this);
266 mCurrentDialog
= confDialog
;
267 mCurrentDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
270 case R
.id
.open_file_item
: {
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
.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
.e(TAG
, "Trying to find out MIME type of a file without extension: " + storagePath
);
301 } catch (ActivityNotFoundException e
) {
302 Log
.e(TAG
, "No activity found to handle: " + storagePath
+ " with MIME type " + mimeType
+ " obtained from extension");
304 } catch (Throwable th
) {
305 Log
.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
.download_file_item
: {
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
.cancel_download_item
: {
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
.cancel_upload_item
: {
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
);
345 return super.onContextItemSelected(item
);
351 * Call this, when the user presses the up button
353 public void onNavigateUp() {
354 OCFile parentDir
= null
;
357 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
358 parentDir
= storageManager
.getFileById(mFile
.getParentId());
361 listDirectory(parentDir
);
365 * Use this to query the {@link OCFile} that is currently
366 * being displayed by this fragment
367 * @return The currently viewed OCFile
369 public OCFile
getCurrentFile(){
374 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
376 public void listDirectory(){
381 * Lists the given directory on the view. When the input parameter is null,
382 * it will either refresh the last known directory, or list the root
383 * if there never was a directory.
385 * @param directory File to be listed
387 public void listDirectory(OCFile directory
) {
388 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
389 if (storageManager
!= null
) {
391 // Check input parameters for null
392 if(directory
== null
){
396 directory
= storageManager
.getFileByPath("/");
397 if (directory
== null
) return; // no files, wait for sync
402 // If that's not a directory -> List its parent
403 if(!directory
.isDirectory()){
404 Log
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
405 directory
= storageManager
.getFileById(directory
.getParentId());
408 mAdapter
.swapDirectory(directory
, storageManager
);
409 if (mFile
== null
|| !mFile
.equals(directory
)) {
410 mList
.setSelectionFromTop(0, 0);
419 * Interface to implement by any Activity that includes some instance of FileListFragment
421 * @author David A. Velasco
423 public interface ContainerActivity
extends TransferServiceGetter
, OnRemoteOperationListener
{
426 * Callback method invoked when a directory is clicked by the user on the files list
430 public void onDirectoryClick(OCFile file
);
433 * Callback method invoked when a file (non directory) is clicked by the user on the files list
437 public void onFileClick(OCFile file
);
440 * Getter for the current DataStorageManager in the container activity
442 public DataStorageManager
getStorageManager();
446 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
448 * @return Directory to list firstly. Can be NULL.
450 public OCFile
getInitialDirectory();
454 * Callback method invoked when a the 'transfer state' of a file changes.
456 * This happens when a download or upload is started or ended for a file.
458 * This method is necessary by now to update the user interface of the double-pane layout in tablets
459 * because methods {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and {@link FileUploaderBinder#isUploading(Account, OCFile)}
460 * won't provide the needed response before the method where this is called finishes.
462 * TODO Remove this when the transfer state of a file is kept in the database (other thing TODO)
464 * @param file OCFile which state changed.
465 * @param downloading Flag signaling if the file is now downloading.
466 * @param uploading Flag signaling if the file is now uploading.
468 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
);
474 public void onDismiss(EditNameDialog dialog
) {
475 if (dialog
.getResult()) {
476 String newFilename
= dialog
.getNewFilename();
477 Log
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
478 RemoteOperation operation
= new RenameFileOperation(mTargetFile
,
479 AccountUtils
.getCurrentOwnCloudAccount(getActivity()),
481 mContainerActivity
.getStorageManager());
482 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity().getApplicationContext());
483 operation
.execute(wc
, mContainerActivity
, mHandler
);
484 getActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
490 public void onConfirmation(String callerTag
) {
491 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
492 if (mContainerActivity
.getStorageManager().getFileById(mTargetFile
.getFileId()) != null
) {
493 RemoteOperation operation
= new RemoveFileOperation( mTargetFile
,
495 mContainerActivity
.getStorageManager());
496 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity().getApplicationContext());
497 operation
.execute(wc
, mContainerActivity
, mHandler
);
499 getActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
501 mCurrentDialog
.dismiss();
502 mCurrentDialog
= null
;
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
);
518 mCurrentDialog
.dismiss();
519 mCurrentDialog
= null
;
521 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
525 public void onCancel(String callerTag
) {
526 Log
.d(TAG
, "REMOVAL CANCELED");
527 mCurrentDialog
.dismiss();
528 mCurrentDialog
= null
;