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
;
165 public boolean onContextItemSelected (MenuItem item
) {
166 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
167 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
168 switch (item
.getItemId()) {
169 case R
.id
.rename_file_item
:
170 EditNameDialog dialog
= EditNameDialog
.newInstance(mTargetFile
.getFileName());
171 dialog
.setOnDismissListener(this);
172 dialog
.show(getFragmentManager(), "nameeditdialog");
173 Log
.d(TAG
, "RENAME SELECTED, item " + info
.id
+ " at position " + info
.position
);
175 case R
.id
.remove_file_item
:
176 int messageStringId
= R
.string
.confirmation_remove_alert
;
177 int posBtnStringId
= R
.string
.confirmation_remove_remote
;
178 int neuBtnStringId
= -1;
179 if (mTargetFile
.isDirectory()) {
180 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
181 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
182 neuBtnStringId
= R
.string
.confirmation_remove_folder_local
;
183 } else if (mTargetFile
.isDown()) {
184 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
185 neuBtnStringId
= R
.string
.confirmation_remove_local
;
187 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
189 new String
[]{mTargetFile
.getFileName()},
192 R
.string
.common_cancel
);
193 confDialog
.setOnConfirmationListener(this);
194 confDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
195 Log
.d(TAG
, "REMOVE SELECTED, item " + info
.id
+ " at position " + info
.position
);
198 return super.onContextItemSelected(item
);
204 * Call this, when the user presses the up button
206 public void onNavigateUp() {
207 OCFile parentDir
= null
;
210 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
211 parentDir
= storageManager
.getFileById(mFile
.getParentId());
214 listDirectory(parentDir
);
218 * Use this to query the {@link OCFile} that is currently
219 * being displayed by this fragment
220 * @return The currently viewed OCFile
222 public OCFile
getCurrentFile(){
227 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
229 public void listDirectory(){
234 * Lists the given directory on the view. When the input parameter is null,
235 * it will either refresh the last known directory, or list the root
236 * if there never was a directory.
238 * @param directory File to be listed
240 public void listDirectory(OCFile directory
) {
241 DataStorageManager storageManager
= mContainerActivity
.getStorageManager();
242 if (storageManager
!= null
) {
244 // Check input parameters for null
245 if(directory
== null
){
249 directory
= storageManager
.getFileByPath("/");
250 if (directory
== null
) return; // no files, wait for sync
255 // If that's not a directory -> List its parent
256 if(!directory
.isDirectory()){
257 Log
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
258 directory
= storageManager
.getFileById(directory
.getParentId());
261 mAdapter
.swapDirectory(directory
, storageManager
);
262 if (mFile
== null
|| !mFile
.equals(directory
)) {
263 mList
.setSelectionFromTop(0, 0);
272 * Interface to implement by any Activity that includes some instance of FileListFragment
274 * @author David A. Velasco
276 public interface ContainerActivity
extends TransferServiceGetter
{
279 * Callback method invoked when a directory is clicked by the user on the files list
283 public void onDirectoryClick(OCFile file
);
286 * Callback method invoked when a file (non directory) is clicked by the user on the files list
290 public void onFileClick(OCFile file
);
293 * Getter for the current DataStorageManager in the container activity
295 public DataStorageManager
getStorageManager();
299 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
301 * @return Directory to list firstly. Can be NULL.
303 public OCFile
getInitialDirectory();
311 public void onDismiss(EditNameDialog dialog
) {
312 if (dialog
.getResult()) {
313 String newFilename
= dialog
.getNewFilename();
314 Log
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
315 RemoteOperation operation
= new RenameFileOperation(mTargetFile
,
317 mContainerActivity
.getStorageManager());
318 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity().getApplicationContext());
319 operation
.execute(wc
, this, mHandler
);
320 getActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
326 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
327 if (operation
instanceof RemoveFileOperation
) {
328 onRemoveFileOperationFinish((RemoveFileOperation
)operation
, result
);
330 } else if (operation
instanceof RenameFileOperation
) {
331 onRenameFileOperationFinish((RenameFileOperation
)operation
, result
);
336 private void onRemoveFileOperationFinish(RemoveFileOperation operation
, RemoteOperationResult result
) {
337 getActivity().dismissDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
338 if (result
.isSuccess()) {
339 Toast msg
= Toast
.makeText(getActivity().getApplicationContext(), R
.string
.remove_success_msg
, Toast
.LENGTH_LONG
);
341 if (mIsLargeLayout
) {
342 // TODO - this should be done only when the current FileDetailFragment shows the deleted file
343 // -> THIS METHOD WOULD BE BETTER PLACED AT THE ACTIVITY LEVEL
344 FragmentTransaction transaction
= getActivity().getSupportFragmentManager().beginTransaction();
345 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(null
, null
)); // empty FileDetailFragment
346 transaction
.commit();
351 Toast msg
= Toast
.makeText(getActivity(), R
.string
.remove_fail_msg
, Toast
.LENGTH_LONG
);
353 if (result
.isSslRecoverableException()) {
354 // TODO show the SSL warning dialog
360 private void onRenameFileOperationFinish(RenameFileOperation operation
, RemoteOperationResult result
) {
361 getActivity().dismissDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
362 if (result
.isSuccess()) {
367 if (result
.getCode().equals(ResultCode
.INVALID_LOCAL_FILE_NAME
)) {
368 Toast msg
= Toast
.makeText(getActivity(), R
.string
.rename_local_fail_msg
, Toast
.LENGTH_LONG
);
370 // TODO throw again the new rename dialog
372 Toast msg
= Toast
.makeText(getActivity(), R
.string
.rename_server_fail_msg
, Toast
.LENGTH_LONG
);
374 if (result
.isSslRecoverableException()) {
375 // TODO show the SSL warning dialog
383 public void onConfirmation(String callerTag
) {
384 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
385 if (mContainerActivity
.getStorageManager().getFileById(mTargetFile
.getFileId()) != null
) {
386 RemoteOperation operation
= new RemoveFileOperation( mTargetFile
,
388 mContainerActivity
.getStorageManager());
389 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity().getApplicationContext());
390 operation
.execute(wc
, this, mHandler
);
392 getActivity().showDialog(FileDisplayActivity
.DIALOG_SHORT_WAIT
);
398 public void onNeutral(String callerTag
) {
400 if (mTargetFile
.isDirectory()) {
401 // TODO run in a secondary thread?
402 mContainerActivity
.getStorageManager().removeDirectory(mTargetFile
, false
, true
);
404 } else if (mTargetFile
.isDown() && (f
= new File(mTargetFile
.getStoragePath())).exists()) {
406 mTargetFile
.setStoragePath(null
);
407 mContainerActivity
.getStorageManager().saveFile(mFile
);
413 public void onCancel(String callerTag
) {
414 Log
.d(TAG
, "REMOVAL CANCELED");