1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2014 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 version 2,
7 * as published by the Free Software Foundation.
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
;
23 import com
.owncloud
.android
.R
;
24 import com
.owncloud
.android
.authentication
.AccountUtils
;
25 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
26 import com
.owncloud
.android
.datamodel
.OCFile
;
27 import com
.owncloud
.android
.files
.FileMenuFilter
;
28 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
29 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
30 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
31 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
32 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
33 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
34 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
;
35 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
36 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
37 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
38 import com
.owncloud
.android
.utils
.Log_OC
;
40 import android
.accounts
.Account
;
41 import android
.app
.Activity
;
42 import android
.os
.Bundle
;
43 import android
.view
.ContextMenu
;
44 import android
.view
.MenuInflater
;
45 import android
.view
.MenuItem
;
46 import android
.view
.View
;
47 import android
.widget
.AdapterView
;
48 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
51 * A Fragment that lists all files and folders in a given path.
53 * TODO refactorize to get rid of direct dependency on FileDisplayActivity
55 * @author Bartek Przybylski
57 * @author David A. Velasco
59 public class OCFileListFragment
extends ExtendedListFragment
60 implements EditNameDialogListener
, ConfirmationDialogFragmentListener
{
62 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
64 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ? OCFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
65 private static final String EXTRA_FILE
= MY_PACKAGE
+ ".extra.FILE";
67 private static final String KEY_INDEXES
= "INDEXES";
68 private static final String KEY_FIRST_POSITIONS
= "FIRST_POSITIONS";
69 private static final String KEY_TOPS
= "TOPS";
70 private static final String KEY_HEIGHT_CELL
= "HEIGHT_CELL";
72 private FileFragment
.ContainerActivity mContainerActivity
;
74 private OCFile mFile
= null
;
75 private FileListListAdapter mAdapter
;
77 private OCFile mTargetFile
;
79 // Save the state of the scroll in browsing
80 private ArrayList
<Integer
> mIndexes
;
81 private ArrayList
<Integer
> mFirstPositions
;
82 private ArrayList
<Integer
> mTops
;
84 private int mHeightCell
= 0;
90 public void onAttach(Activity activity
) {
91 super.onAttach(activity
);
92 Log_OC
.e(TAG
, "onAttach");
94 mContainerActivity
= (FileFragment
.ContainerActivity
) activity
;
95 } catch (ClassCastException e
) {
96 throw new ClassCastException(activity
.toString() + " must implement " +
97 FileFragment
.ContainerActivity
.class.getSimpleName());
103 public void onDetach() {
104 mContainerActivity
= null
;
112 public void onActivityCreated(Bundle savedInstanceState
) {
113 super.onActivityCreated(savedInstanceState
);
114 Log_OC
.e(TAG
, "onActivityCreated() start");
115 mAdapter
= new FileListListAdapter(getSherlockActivity(), mContainerActivity
);
117 if (savedInstanceState
!= null
) {
118 mFile
= savedInstanceState
.getParcelable(EXTRA_FILE
);
119 mIndexes
= savedInstanceState
.getIntegerArrayList(KEY_INDEXES
);
120 mFirstPositions
= savedInstanceState
.getIntegerArrayList(KEY_FIRST_POSITIONS
);
121 mTops
= savedInstanceState
.getIntegerArrayList(KEY_TOPS
);
122 mHeightCell
= savedInstanceState
.getInt(KEY_HEIGHT_CELL
);
125 mIndexes
= new ArrayList
<Integer
>();
126 mFirstPositions
= new ArrayList
<Integer
>();
127 mTops
= new ArrayList
<Integer
>();
132 mAdapter
= new FileListListAdapter(getSherlockActivity(), mContainerActivity
);
134 setListAdapter(mAdapter
);
136 registerForContextMenu(getListView());
137 getListView().setOnCreateContextMenuListener(this);
139 // mHandler = new Handler();
143 * Saves the current listed folder.
146 public void onSaveInstanceState (Bundle outState
) {
147 super.onSaveInstanceState(outState
);
148 outState
.putParcelable(EXTRA_FILE
, mFile
);
149 outState
.putIntegerArrayList(KEY_INDEXES
, mIndexes
);
150 outState
.putIntegerArrayList(KEY_FIRST_POSITIONS
, mFirstPositions
);
151 outState
.putIntegerArrayList(KEY_TOPS
, mTops
);
152 outState
.putInt(KEY_HEIGHT_CELL
, mHeightCell
);
156 * Call this, when the user presses the up button.
158 * Tries to move up the current folder one level. If the parent folder was removed from the database,
159 * it continues browsing up until finding an existing folders.
161 * return Count of folder levels browsed up.
163 public int onBrowseUp() {
164 OCFile parentDir
= null
;
168 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
170 String parentPath
= null
;
171 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
172 parentPath
= new File(mFile
.getRemotePath()).getParent();
173 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
174 parentDir
= storageManager
.getFileByPath(parentPath
);
177 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
); // never returns null; keep the path in root folder
179 while (parentDir
== null
) {
180 parentPath
= new File(parentPath
).getParent();
181 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
182 parentDir
= storageManager
.getFileByPath(parentPath
);
184 } // exit is granted because storageManager.getFileByPath("/") never returns null
189 listDirectory(mFile
);
191 ((FileDisplayActivity
)mContainerActivity
).startSyncFolderOperation(mFile
);
193 // restore index and top position
194 restoreIndexAndTopPosition();
196 } // else - should never happen now
202 * Restore index and position
204 private void restoreIndexAndTopPosition() {
205 if (mIndexes
.size() > 0) {
206 // needs to be checked; not every browse-up had a browse-down before
208 int index
= mIndexes
.remove(mIndexes
.size() - 1);
210 int firstPosition
= mFirstPositions
.remove(mFirstPositions
.size() -1);
212 int top
= mTops
.remove(mTops
.size() - 1);
214 mList
.setSelectionFromTop(firstPosition
, top
);
216 // Move the scroll if the selection is not visible
217 int indexPosition
= mHeightCell
*index
;
218 int height
= mList
.getHeight();
220 if (indexPosition
> height
) {
221 if (android
.os
.Build
.VERSION
.SDK_INT
>= 11)
223 mList
.smoothScrollToPosition(index
);
225 else if (android
.os
.Build
.VERSION
.SDK_INT
>= 8)
227 mList
.setSelectionFromTop(index
, 0);
235 * Save index and top position
237 private void saveIndexAndTopPosition(int index
) {
241 int firstPosition
= mList
.getFirstVisiblePosition();
242 mFirstPositions
.add(firstPosition
);
244 View view
= mList
.getChildAt(0);
245 int top
= (view
== null
) ?
0 : view
.getTop() ;
249 // Save the height of a cell
250 mHeightCell
= (view
== null
|| mHeightCell
!= 0) ? mHeightCell
: view
.getHeight();
254 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
255 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
257 if (file
.isFolder()) {
258 // update state and view of this fragment
260 // then, notify parent activity to let it update its state and view, and other fragments
261 mContainerActivity
.onBrowsedDownTo(file
);
262 // save index and top position
263 saveIndexAndTopPosition(position
);
265 } else { /// Click on a file
266 if (PreviewImageFragment
.canBePreviewed(file
)) {
267 // preview image - it handles the download, if needed
268 ((FileDisplayActivity
)mContainerActivity
).startImagePreview(file
);
270 } else if (file
.isDown()) {
271 if (PreviewMediaFragment
.canBePreviewed(file
)) {
273 ((FileDisplayActivity
)mContainerActivity
).startMediaPreview(file
, 0, true
);
275 mContainerActivity
.getFileOperationsHelper().openFile(file
);
279 // automatic download, preview on finish
280 ((FileDisplayActivity
)mContainerActivity
).startDownloadForPreview(file
);
286 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
295 public void onCreateContextMenu (ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
296 super.onCreateContextMenu(menu
, v
, menuInfo
);
297 MenuInflater inflater
= getSherlockActivity().getMenuInflater();
298 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
299 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
300 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
302 FileMenuFilter mf
= new FileMenuFilter();
303 mf
.setFile(targetFile
);
304 mf
.setComponentGetter(mContainerActivity
);
305 mf
.setAccount(mContainerActivity
.getStorageManager().getAccount());
306 mf
.setContext(getSherlockActivity());
307 mf
.setFragment(this);
316 public boolean onContextItemSelected (MenuItem item
) {
317 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
318 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
319 switch (item
.getItemId()) {
320 case R
.id
.action_share_file
: {
321 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
324 case R
.id
.action_unshare_file
: {
325 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
328 case R
.id
.action_rename_file
: {
329 String fileName
= mTargetFile
.getFileName();
330 int extensionStart
= mTargetFile
.isFolder() ?
-1 : fileName
.lastIndexOf(".");
331 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: fileName
.length();
332 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), fileName
, 0, selectionEnd
, this);
333 dialog
.show(getFragmentManager(), EditNameDialog
.TAG
);
336 case R
.id
.action_remove_file
: {
337 int messageStringId
= R
.string
.confirmation_remove_alert
;
338 int posBtnStringId
= R
.string
.confirmation_remove_remote
;
339 int neuBtnStringId
= -1;
340 if (mTargetFile
.isFolder()) {
341 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
342 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
343 neuBtnStringId
= R
.string
.confirmation_remove_folder_local
;
344 } else if (mTargetFile
.isDown()) {
345 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
346 neuBtnStringId
= R
.string
.confirmation_remove_local
;
348 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
350 new String
[]{mTargetFile
.getFileName()},
353 R
.string
.common_cancel
);
354 confDialog
.setOnConfirmationListener(this);
355 confDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
358 case R
.id
.action_sync_file
: {
359 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
362 case R
.id
.action_cancel_download
: {
363 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
364 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
365 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, mTargetFile
)) {
366 downloaderBinder
.cancel(account
, mTargetFile
);
368 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
372 case R
.id
.action_cancel_upload
: {
373 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
374 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
375 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, mTargetFile
)) {
376 uploaderBinder
.cancel(account
, mTargetFile
);
378 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
382 case R
.id
.action_see_details
: {
383 mContainerActivity
.showDetails(mTargetFile
);
386 case R
.id
.action_send_file
: {
388 if (!mTargetFile
.isDown()) { // Download the file
389 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
390 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(mTargetFile
);
393 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(mTargetFile
);
398 return super.onContextItemSelected(item
);
404 * Use this to query the {@link OCFile} that is currently
405 * being displayed by this fragment
406 * @return The currently viewed OCFile
408 public OCFile
getCurrentFile(){
413 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
415 public void listDirectory(){
420 * Lists the given directory on the view. When the input parameter is null,
421 * it will either refresh the last known directory. list the root
422 * if there never was a directory.
424 * @param directory File to be listed
426 public void listDirectory(OCFile directory
) {
427 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
428 if (storageManager
!= null
) {
430 // Check input parameters for null
431 if(directory
== null
){
435 directory
= storageManager
.getFileByPath("/");
436 if (directory
== null
) return; // no files, wait for sync
441 // If that's not a directory -> List its parent
442 if(!directory
.isFolder()){
443 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
444 directory
= storageManager
.getFileById(directory
.getParentId());
447 mAdapter
.swapDirectory(directory
, storageManager
);
448 if (mFile
== null
|| !mFile
.equals(directory
)) {
449 mList
.setSelectionFromTop(0, 0);
458 public void onDismiss(EditNameDialog dialog
) {
459 if (dialog
.getResult()) {
460 String newFilename
= dialog
.getNewFilename();
461 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
462 mContainerActivity
.getFileOperationsHelper().renameFile(mTargetFile
, newFilename
);
468 public void onConfirmation(String callerTag
) {
469 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
470 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
471 if (storageManager
.getFileById(mTargetFile
.getFileId()) != null
) {
472 mContainerActivity
.getFileOperationsHelper().removeFile(mTargetFile
, true
);
478 public void onNeutral(String callerTag
) {
479 mContainerActivity
.getStorageManager().removeFile(mTargetFile
, false
, true
); // TODO perform in background task / new thread
481 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
485 public void onCancel(String callerTag
) {
486 Log_OC
.d(TAG
, "REMOVAL CANCELED");