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
.network
.OwnCloudClientUtils
;
27 import com
.owncloud
.android
.operations
.OnRemoteOperationListener
;
28 import com
.owncloud
.android
.operations
.RemoteOperation
;
29 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
30 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
31 import com
.owncloud
.android
.operations
.RenameFileOperation
;
32 import com
.owncloud
.android
.operations
.RemoteOperationResult
.ResultCode
;
33 import com
.owncloud
.android
.ui
.FragmentListView
;
34 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
35 import com
.owncloud
.android
.ui
.activity
.TransferServiceGetter
;
36 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
37 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
38 import com
.owncloud
.android
.ui
.fragment
.ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
;
40 import eu
.alefzero
.webdav
.WebdavClient
;
42 import android
.app
.Activity
;
43 import android
.os
.Bundle
;
44 import android
.os
.Handler
;
45 import android
.support
.v4
.app
.FragmentTransaction
;
46 import android
.util
.Log
;
47 import android
.view
.ContextMenu
;
48 import android
.view
.MenuInflater
;
49 import android
.view
.MenuItem
;
50 import android
.view
.View
;
51 import android
.widget
.AdapterView
;
52 import android
.widget
.Toast
;
53 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
56 * A Fragment that lists all files and folders in a given path.
58 * @author Bartek Przybylski
61 public class OCFileListFragment
extends FragmentListView
implements EditNameDialog
.EditNameDialogListener
, OnRemoteOperationListener
, ConfirmationDialogFragmentListener
{
62 private static final String TAG
= "FileListFragment";
63 private static final String SAVED_LIST_POSITION
= "LIST_POSITION";
65 private OCFileListFragment
.ContainerActivity mContainerActivity
;
67 private OCFile mFile
= null
;
68 private FileListListAdapter mAdapter
;
70 private Handler mHandler
;
71 private boolean mIsLargeLayout
;
72 private OCFile mTargetFile
;
79 public void onAttach(Activity activity
) {
80 super.onAttach(activity
);
82 mContainerActivity
= (ContainerActivity
) activity
;
83 } catch (ClassCastException e
) {
84 throw new ClassCastException(activity
.toString() + " must implement " + OCFileListFragment
.ContainerActivity
.class.getSimpleName());
93 public void onActivityCreated(Bundle savedInstanceState
) {
94 Log
.i(TAG
, "onActivityCreated() start");
96 super.onActivityCreated(savedInstanceState
);
97 mAdapter
= new FileListListAdapter(mContainerActivity
.getInitialDirectory(), mContainerActivity
.getStorageManager(), getActivity(), mContainerActivity
);
98 setListAdapter(mAdapter
);
100 if (savedInstanceState
!= null
) {
101 Log
.i(TAG
, "savedInstanceState is not null");
102 int position
= savedInstanceState
.getInt(SAVED_LIST_POSITION
);
103 setReferencePosition(position
);
106 registerForContextMenu(getListView());
107 getListView().setOnCreateContextMenuListener(this);
109 mIsLargeLayout
= getResources().getBoolean(R
.bool
.large_layout
);
110 mHandler
= new Handler();
112 Log
.i(TAG
, "onActivityCreated() stop");
118 public void onSaveInstanceState(Bundle savedInstanceState
) {
119 Log
.i(TAG
, "onSaveInstanceState() start");
121 savedInstanceState
.putInt(SAVED_LIST_POSITION
, getReferencePosition());
123 Log
.i(TAG
, "onSaveInstanceState() stop");
128 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
129 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
131 /// Click on a directory
132 if (file
.getMimetype().equals("DIR")) {
133 // just local updates
136 // any other updates are let to the container Activity
137 mContainerActivity
.onDirectoryClick(file
);
139 } else { /// Click on a file
140 mContainerActivity
.onFileClick(file
);
144 Log
.d(TAG
, "Null object in ListAdapter!!");
153 public void onCreateContextMenu (ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
154 super.onCreateContextMenu(menu
, v
, menuInfo
);
155 MenuInflater inflater
= getActivity().getMenuInflater();
156 inflater
.inflate(R
.menu
.file_context_menu
, menu
);
157 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
158 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
159 MenuItem item
= null
;
161 if (targetFile
.isDirectory()) {
162 int[] theIds
= {R
.id
.open_file_item
, R
.id
.download_file_item
, R
.id
.cancel_download_item
, R
.id
.cancel_upload_item
};
165 } else if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
166 int[] theIds
= {R
.id
.open_file_item
, R
.id
.download_file_item
, R
.id
.cancel_upload_item
};
169 } else if ( mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getActivity()), targetFile
)) {
170 int[] theIds
= {R
.id
.open_file_item
, R
.id
.download_file_item
, R
.id
.cancel_download_item
};
173 } else if ( targetFile
.isDown()) {
174 int[] theIds
= {R
.id
.cancel_download_item
, R
.id
.cancel_upload_item
};
178 int[] theIds
= {R
.id
.open_file_item
, R
.id
.cancel_download_item
, R
.id
.cancel_upload_item
};
182 for (int i
=0; i
< ids
.length
; i
++) {
183 item
= menu
.findItem(ids
[i
]);
185 item
.setVisible(false
);
186 item
.setEnabled(false
);
196 public boolean onContextItemSelected (MenuItem item
) {
197 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
198 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
199 switch (item
.getItemId()) {
200 case R
.id
.rename_file_item
:
201 EditNameDialog dialog
= EditNameDialog
.newInstance(mTargetFile
.getFileName());
202 dialog
.setOnDismissListener(this);
203 dialog
.show(getFragmentManager(), "nameeditdialog");
204 Log
.d(TAG
, "RENAME SELECTED, item " + info
.id
+ " at position " + info
.position
);
206 case R
.id
.remove_file_item
:
207 int messageStringId
= R
.string
.confirmation_remove_alert
;
208 int posBtnStringId
= R
.string
.confirmation_remove_remote
;
209 int neuBtnStringId
= -1;
210 if (mTargetFile
.isDirectory()) {
211 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
212 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
213 neuBtnStringId
= R
.string
.confirmation_remove_folder_local
;
214 } else if (mTargetFile
.isDown()) {
215 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
216 neuBtnStringId
= R
.string
.confirmation_remove_local
;
218 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
220 new String
[]{mTargetFile
.getFileName()},
223 R
.string
.common_cancel
);
224 confDialog
.setOnConfirmationListener(this);
225 confDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
226 Log
.d(TAG
, "REMOVE SELECTED, item " + info
.id
+ " at position " + info
.position
);
229 return super.onContextItemSelected(item
);
235 * Call this, when the user presses the up button
237 public void onNavigateUp() {
238 OCFile parentDir
= null
;
241 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
242 parentDir
= storageManager
.getFileById(mFile
.getParentId());
245 listDirectory(parentDir
);
249 * Use this to query the {@link OCFile} that is currently
250 * being displayed by this fragment
251 * @return The currently viewed OCFile
253 public OCFile
getCurrentFile(){
258 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
260 public void listDirectory(){
265 * Lists the given directory on the view. When the input parameter is null,
266 * it will either refresh the last known directory, or list the root
267 * if there never was a directory.
269 * @param directory File to be listed
271 public void listDirectory(OCFile directory
) {
272 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
273 if (storageManager
!= null
) {
275 // Check input parameters for null
276 if(directory
== null
){
280 directory
= storageManager
.getFileByPath("/");
281 if (directory
== null
) return; // no files, wait for sync
286 // If that's not a directory -> List its parent
287 if(!directory
.isDirectory()){
288 Log
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
289 directory
= storageManager
.getFileById(directory
.getParentId());
292 mAdapter
.swapDirectory(directory
, storageManager
);
293 if (mFile
== null
|| !mFile
.equals(directory
)) {
294 mList
.setSelectionFromTop(0, 0);
303 * Interface to implement by any Activity that includes some instance of FileListFragment
305 * @author David A. Velasco
307 public interface ContainerActivity
extends TransferServiceGetter
{
310 * Callback method invoked when a directory is clicked by the user on the files list
314 public void onDirectoryClick(OCFile file
);
317 * Callback method invoked when a file (non directory) is clicked by the user on the files list
321 public void onFileClick(OCFile file
);
324 * Getter for the current DataStorageManager in the container activity
326 public DataStorageManager
getStorageManager();
330 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
332 * @return Directory to list firstly. Can be NULL.
334 public OCFile
getInitialDirectory();
342 public void onDismiss(EditNameDialog dialog
) {
343 if (dialog
.getResult()) {
344 String newFilename
= dialog
.getNewFilename();
345 Log
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
346 RemoteOperation operation
= new RenameFileOperation(mTargetFile
,
348 mContainerActivity
.getStorageManager());
349 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity().getApplicationContext());
350 operation
.execute(wc
, this, mHandler
);
351 getActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
357 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
358 if (operation
instanceof RemoveFileOperation
) {
359 onRemoveFileOperationFinish((RemoveFileOperation
)operation
, result
);
361 } else if (operation
instanceof RenameFileOperation
) {
362 onRenameFileOperationFinish((RenameFileOperation
)operation
, result
);
367 private void onRemoveFileOperationFinish(RemoveFileOperation operation
, RemoteOperationResult result
) {
368 getActivity().dismissDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
369 if (result
.isSuccess()) {
370 Toast msg
= Toast
.makeText(getActivity().getApplicationContext(), R
.string
.remove_success_msg
, Toast
.LENGTH_LONG
);
372 if (mIsLargeLayout
) {
373 // TODO - this should be done only when the current FileDetailFragment shows the deleted file
374 // -> THIS METHOD WOULD BE BETTER PLACED AT THE ACTIVITY LEVEL
375 FragmentTransaction transaction
= getActivity().getSupportFragmentManager().beginTransaction();
376 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(null
, null
)); // empty FileDetailFragment
377 transaction
.commit();
382 Toast msg
= Toast
.makeText(getActivity(), R
.string
.remove_fail_msg
, Toast
.LENGTH_LONG
);
384 if (result
.isSslRecoverableException()) {
385 // TODO show the SSL warning dialog
391 private void onRenameFileOperationFinish(RenameFileOperation operation
, RemoteOperationResult result
) {
392 getActivity().dismissDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
393 if (result
.isSuccess()) {
398 if (result
.getCode().equals(ResultCode
.INVALID_LOCAL_FILE_NAME
)) {
399 Toast msg
= Toast
.makeText(getActivity(), R
.string
.rename_local_fail_msg
, Toast
.LENGTH_LONG
);
401 // TODO throw again the new rename dialog
403 Toast msg
= Toast
.makeText(getActivity(), R
.string
.rename_server_fail_msg
, Toast
.LENGTH_LONG
);
405 if (result
.isSslRecoverableException()) {
406 // TODO show the SSL warning dialog
414 public void onConfirmation(String callerTag
) {
415 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
416 if (mContainerActivity
.getStorageManager().getFileById(mTargetFile
.getFileId()) != null
) {
417 RemoteOperation operation
= new RemoveFileOperation( mTargetFile
,
419 mContainerActivity
.getStorageManager());
420 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity().getApplicationContext());
421 operation
.execute(wc
, this, mHandler
);
423 getActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
429 public void onNeutral(String callerTag
) {
431 if (mTargetFile
.isDirectory()) {
432 // TODO run in a secondary thread?
433 mContainerActivity
.getStorageManager().removeDirectory(mTargetFile
, false
, true
);
435 } else if (mTargetFile
.isDown() && (f
= new File(mTargetFile
.getStoragePath())).exists()) {
437 mTargetFile
.setStoragePath(null
);
438 mContainerActivity
.getStorageManager().saveFile(mFile
);
444 public void onCancel(String callerTag
) {
445 Log
.d(TAG
, "REMOVAL CANCELED");