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
;
22 import com
.owncloud
.android
.AccountUtils
;
23 import com
.owncloud
.android
.R
;
24 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
25 import com
.owncloud
.android
.datamodel
.OCFile
;
26 import com
.owncloud
.android
.files
.services
.FileDownloader
;
27 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
28 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
29 import com
.owncloud
.android
.network
.OwnCloudClientUtils
;
30 import com
.owncloud
.android
.operations
.OnRemoteOperationListener
;
31 import com
.owncloud
.android
.operations
.RemoteOperation
;
32 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
33 import com
.owncloud
.android
.operations
.RenameFileOperation
;
34 import com
.owncloud
.android
.ui
.FragmentListView
;
35 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
36 import com
.owncloud
.android
.ui
.activity
.TransferServiceGetter
;
37 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
38 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
39 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
40 import com
.owncloud
.android
.ui
.fragment
.ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
;
42 import eu
.alefzero
.webdav
.WebdavClient
;
43 import eu
.alefzero
.webdav
.WebdavUtils
;
45 import android
.accounts
.Account
;
46 import android
.app
.Activity
;
47 import android
.content
.ActivityNotFoundException
;
48 import android
.content
.Intent
;
49 import android
.net
.Uri
;
50 import android
.os
.Bundle
;
51 import android
.os
.Handler
;
52 import android
.util
.Log
;
53 import android
.view
.ContextMenu
;
54 import android
.view
.MenuInflater
;
55 import android
.view
.MenuItem
;
56 import android
.view
.View
;
57 import android
.webkit
.MimeTypeMap
;
58 import android
.widget
.AdapterView
;
59 import android
.widget
.Toast
;
60 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
63 * A Fragment that lists all files and folders in a given path.
65 * @author Bartek Przybylski
68 public class OCFileListFragment
extends FragmentListView
implements EditNameDialogListener
, ConfirmationDialogFragmentListener
{
69 private static final String TAG
= "FileListFragment";
70 private static final String SAVED_LIST_POSITION
= "LIST_POSITION";
72 private OCFileListFragment
.ContainerActivity mContainerActivity
;
74 private OCFile mFile
= null
;
75 private FileListListAdapter mAdapter
;
77 private Handler mHandler
;
78 private OCFile mTargetFile
;
85 public void onAttach(Activity activity
) {
86 super.onAttach(activity
);
88 mContainerActivity
= (ContainerActivity
) activity
;
89 } catch (ClassCastException e
) {
90 throw new ClassCastException(activity
.toString() + " must implement " + OCFileListFragment
.ContainerActivity
.class.getSimpleName());
99 public void onActivityCreated(Bundle savedInstanceState
) {
100 Log
.i(TAG
, "onActivityCreated() start");
102 super.onActivityCreated(savedInstanceState
);
103 mAdapter
= new FileListListAdapter(mContainerActivity
.getInitialDirectory(), mContainerActivity
.getStorageManager(), getActivity(), mContainerActivity
);
104 setListAdapter(mAdapter
);
106 if (savedInstanceState
!= null
) {
107 Log
.i(TAG
, "savedInstanceState is not null");
108 int position
= savedInstanceState
.getInt(SAVED_LIST_POSITION
);
109 setReferencePosition(position
);
112 registerForContextMenu(getListView());
113 getListView().setOnCreateContextMenuListener(this);
115 mHandler
= new Handler();
117 Log
.i(TAG
, "onActivityCreated() stop");
123 public void onSaveInstanceState(Bundle savedInstanceState
) {
124 Log
.i(TAG
, "onSaveInstanceState() start");
126 savedInstanceState
.putInt(SAVED_LIST_POSITION
, getReferencePosition());
128 Log
.i(TAG
, "onSaveInstanceState() stop");
133 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
134 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
136 /// Click on a directory
137 if (file
.getMimetype().equals("DIR")) {
138 // just local updates
141 // any other updates are let to the container Activity
142 mContainerActivity
.onDirectoryClick(file
);
144 } else { /// Click on a file
145 mContainerActivity
.onFileClick(file
);
149 Log
.d(TAG
, "Null object in ListAdapter!!");
158 public void onCreateContextMenu (ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
159 super.onCreateContextMenu(menu
, v
, menuInfo
);
160 MenuInflater inflater
= getActivity().getMenuInflater();
161 inflater
.inflate(R
.menu
.file_context_menu
, menu
);
162 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
163 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
164 MenuItem item
= null
;
166 if (targetFile
.isDirectory()) {
167 int[] theIds
= {R
.id
.open_file_item
, R
.id
.download_file_item
, R
.id
.cancel_download_item
, R
.id
.cancel_upload_item
};
170 } else if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
171 int[] theIds
= {R
.id
.open_file_item
, R
.id
.download_file_item
, R
.id
.cancel_upload_item
};
174 } else if ( mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
175 int[] theIds
= {R
.id
.open_file_item
, R
.id
.download_file_item
, R
.id
.cancel_download_item
};
178 } else if ( targetFile
.isDown()) {
179 int[] theIds
= {R
.id
.cancel_download_item
, R
.id
.cancel_upload_item
};
183 int[] theIds
= {R
.id
.open_file_item
, R
.id
.cancel_download_item
, R
.id
.cancel_upload_item
};
187 for (int i
=0; i
< ids
.length
; i
++) {
188 item
= menu
.findItem(ids
[i
]);
190 item
.setVisible(false
);
191 item
.setEnabled(false
);
201 public boolean onContextItemSelected (MenuItem item
) {
202 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
203 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
204 switch (item
.getItemId()) {
205 case R
.id
.rename_file_item
: {
206 EditNameDialog dialog
= EditNameDialog
.newInstance(mTargetFile
.getFileName());
207 dialog
.setOnDismissListener(this);
208 dialog
.show(getFragmentManager(), EditNameDialog
.TAG
);
211 case R
.id
.remove_file_item
: {
212 int messageStringId
= R
.string
.confirmation_remove_alert
;
213 int posBtnStringId
= R
.string
.confirmation_remove_remote
;
214 int neuBtnStringId
= -1;
215 if (mTargetFile
.isDirectory()) {
216 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
217 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
218 neuBtnStringId
= R
.string
.confirmation_remove_folder_local
;
219 } else if (mTargetFile
.isDown()) {
220 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
221 neuBtnStringId
= R
.string
.confirmation_remove_local
;
223 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
225 new String
[]{mTargetFile
.getFileName()},
228 R
.string
.common_cancel
);
229 confDialog
.setOnConfirmationListener(this);
230 confDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
233 case R
.id
.open_file_item
: {
234 String storagePath
= mTargetFile
.getStoragePath();
235 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
237 Intent i
= new Intent(Intent
.ACTION_VIEW
);
238 i
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), mTargetFile
.getMimetype());
239 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
242 } catch (Throwable t
) {
243 Log
.e(TAG
, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mTargetFile
.getMimetype());
244 boolean toastIt
= true
;
245 String mimeType
= "";
247 Intent i
= new Intent(Intent
.ACTION_VIEW
);
248 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
249 if (mimeType
== null
|| !mimeType
.equals(mTargetFile
.getMimetype())) {
250 if (mimeType
!= null
) {
251 i
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), mimeType
);
254 i
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), "*/*");
256 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
261 } catch (IndexOutOfBoundsException e
) {
262 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + storagePath
);
264 } catch (ActivityNotFoundException e
) {
265 Log
.e(TAG
, "No activity found to handle: " + storagePath
+ " with MIME type " + mimeType
+ " obtained from extension");
267 } catch (Throwable th
) {
268 Log
.e(TAG
, "Unexpected problem when opening: " + storagePath
, th
);
272 Toast
.makeText(getActivity(), "There is no application to handle file " + mTargetFile
.getFileName(), Toast
.LENGTH_SHORT
).show();
279 case R
.id
.download_file_item
: {
280 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
281 Intent i
= new Intent(getActivity(), FileDownloader
.class);
282 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
283 i
.putExtra(FileDownloader
.EXTRA_FILE
, mTargetFile
);
284 getActivity().startService(i
);
286 mContainerActivity
.onTransferStateChanged(mTargetFile
, true
, false
);
289 case R
.id
.cancel_download_item
: {
290 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
291 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
292 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, mTargetFile
)) {
293 downloaderBinder
.cancel(account
, mTargetFile
);
295 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
299 case R
.id
.cancel_upload_item
: {
300 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
301 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
302 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, mTargetFile
)) {
303 uploaderBinder
.cancel(account
, mTargetFile
);
305 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
310 return super.onContextItemSelected(item
);
316 * Call this, when the user presses the up button
318 public void onNavigateUp() {
319 OCFile parentDir
= null
;
322 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
323 parentDir
= storageManager
.getFileById(mFile
.getParentId());
326 listDirectory(parentDir
);
330 * Use this to query the {@link OCFile} that is currently
331 * being displayed by this fragment
332 * @return The currently viewed OCFile
334 public OCFile
getCurrentFile(){
339 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
341 public void listDirectory(){
346 * Lists the given directory on the view. When the input parameter is null,
347 * it will either refresh the last known directory, or list the root
348 * if there never was a directory.
350 * @param directory File to be listed
352 public void listDirectory(OCFile directory
) {
353 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
354 if (storageManager
!= null
) {
356 // Check input parameters for null
357 if(directory
== null
){
361 directory
= storageManager
.getFileByPath("/");
362 if (directory
== null
) return; // no files, wait for sync
367 // If that's not a directory -> List its parent
368 if(!directory
.isDirectory()){
369 Log
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
370 directory
= storageManager
.getFileById(directory
.getParentId());
373 mAdapter
.swapDirectory(directory
, storageManager
);
374 if (mFile
== null
|| !mFile
.equals(directory
)) {
375 mList
.setSelectionFromTop(0, 0);
384 * Interface to implement by any Activity that includes some instance of FileListFragment
386 * @author David A. Velasco
388 public interface ContainerActivity
extends TransferServiceGetter
, OnRemoteOperationListener
{
391 * Callback method invoked when a directory is clicked by the user on the files list
395 public void onDirectoryClick(OCFile file
);
398 * Callback method invoked when a file (non directory) is clicked by the user on the files list
402 public void onFileClick(OCFile file
);
405 * Getter for the current DataStorageManager in the container activity
407 public DataStorageManager
getStorageManager();
411 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
413 * @return Directory to list firstly. Can be NULL.
415 public OCFile
getInitialDirectory();
419 * Callback method invoked when a the 'transfer state' of a file changes.
421 * This happens when a download or upload is started or ended for a file.
423 * This method is necessary by now to update the user interface of the double-pane layout in tablets
424 * because methods {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and {@link FileUploaderBinder#isUploading(Account, OCFile)}
425 * won't provide the needed response before the method where this is called finishes.
427 * TODO Remove this when the transfer state of a file is kept in the database (other thing TODO)
429 * @param file OCFile which state changed.
430 * @param downloading Flag signaling if the file is now downloading.
431 * @param uploading Flag signaling if the file is now uploading.
433 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
);
439 public void onDismiss(EditNameDialog dialog
) {
440 if (dialog
.getResult()) {
441 String newFilename
= dialog
.getNewFilename();
442 Log
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
443 RemoteOperation operation
= new RenameFileOperation(mTargetFile
,
444 AccountUtils
.getCurrentOwnCloudAccount(getActivity()),
446 mContainerActivity
.getStorageManager());
447 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity().getApplicationContext());
448 operation
.execute(wc
, mContainerActivity
, mHandler
);
449 getActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
455 public void onConfirmation(String callerTag
) {
456 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
457 if (mContainerActivity
.getStorageManager().getFileById(mTargetFile
.getFileId()) != null
) {
458 RemoteOperation operation
= new RemoveFileOperation( mTargetFile
,
460 mContainerActivity
.getStorageManager());
461 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity().getApplicationContext());
462 operation
.execute(wc
, mContainerActivity
, mHandler
);
464 getActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
470 public void onNeutral(String callerTag
) {
472 if (mTargetFile
.isDirectory()) {
473 // TODO run in a secondary thread?
474 mContainerActivity
.getStorageManager().removeDirectory(mTargetFile
, false
, true
);
476 } else if (mTargetFile
.isDown() && (f
= new File(mTargetFile
.getStoragePath())).exists()) {
478 mTargetFile
.setStoragePath(null
);
479 mContainerActivity
.getStorageManager().saveFile(mTargetFile
);
482 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
486 public void onCancel(String callerTag
) {
487 Log
.d(TAG
, "REMOVAL CANCELED");