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
;
22 import java
.util
.List
;
24 import com
.owncloud
.android
.R
;
25 import com
.owncloud
.android
.authentication
.AccountUtils
;
26 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
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
.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
.database
.Cursor
;
43 import android
.os
.Bundle
;
44 import android
.view
.ContextMenu
;
45 import android
.view
.MenuInflater
;
46 import android
.view
.MenuItem
;
47 import android
.view
.View
;
48 import android
.widget
.AdapterView
;
49 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
52 * A Fragment that lists all files and folders in a given path.
54 * TODO refactorize to get rid of direct dependency on FileDisplayActivity
56 * @author Bartek Przybylski
58 * @author David A. Velasco
60 public class OCFileListFragment
extends ExtendedListFragment
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");
116 mAdapter
= new FileListListAdapter(getSherlockActivity(), mContainerActivity
);
118 if (savedInstanceState
!= null
) {
119 mFile
= savedInstanceState
.getParcelable(EXTRA_FILE
);
120 mIndexes
= savedInstanceState
.getIntegerArrayList(KEY_INDEXES
);
121 mFirstPositions
= savedInstanceState
.getIntegerArrayList(KEY_FIRST_POSITIONS
);
122 mTops
= savedInstanceState
.getIntegerArrayList(KEY_TOPS
);
123 mHeightCell
= savedInstanceState
.getInt(KEY_HEIGHT_CELL
);
126 mIndexes
= new ArrayList
<Integer
>();
127 mFirstPositions
= new ArrayList
<Integer
>();
128 mTops
= new ArrayList
<Integer
>();
133 setListAdapter(mAdapter
);
135 registerForContextMenu(getListView());
136 getListView().setOnCreateContextMenuListener(this);
138 // mHandler = new Handler();
142 * Saves the current listed folder.
145 public void onSaveInstanceState (Bundle outState
) {
146 super.onSaveInstanceState(outState
);
147 outState
.putParcelable(EXTRA_FILE
, mFile
);
148 outState
.putIntegerArrayList(KEY_INDEXES
, mIndexes
);
149 outState
.putIntegerArrayList(KEY_FIRST_POSITIONS
, mFirstPositions
);
150 outState
.putIntegerArrayList(KEY_TOPS
, mTops
);
151 outState
.putInt(KEY_HEIGHT_CELL
, mHeightCell
);
155 * Call this, when the user presses the up button.
157 * Tries to move up the current folder one level. If the parent folder was removed from the database,
158 * it continues browsing up until finding an existing folders.
160 * return Count of folder levels browsed up.
162 public int onBrowseUp() {
163 OCFile parentDir
= null
;
167 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
169 String parentPath
= null
;
170 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
171 parentPath
= new File(mFile
.getRemotePath()).getParent();
172 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
173 parentDir
= storageManager
.getFileByPath(parentPath
);
176 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
); // never returns null; keep the path in root folder
178 while (parentDir
== null
) {
179 parentPath
= new File(parentPath
).getParent();
180 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
181 parentDir
= storageManager
.getFileByPath(parentPath
);
183 } // exit is granted because storageManager.getFileByPath("/") never returns null
188 listDirectory(mFile
);
190 ((FileDisplayActivity
)mContainerActivity
).startSyncFolderOperation(mFile
);
192 // restore index and top position
193 restoreIndexAndTopPosition();
195 } // else - should never happen now
201 * Restore index and position
203 private void restoreIndexAndTopPosition() {
204 if (mIndexes
.size() > 0) {
205 // needs to be checked; not every browse-up had a browse-down before
207 int index
= mIndexes
.remove(mIndexes
.size() - 1);
209 int firstPosition
= mFirstPositions
.remove(mFirstPositions
.size() -1);
211 int top
= mTops
.remove(mTops
.size() - 1);
213 mList
.setSelectionFromTop(firstPosition
, top
);
215 // Move the scroll if the selection is not visible
216 int indexPosition
= mHeightCell
*index
;
217 int height
= mList
.getHeight();
219 if (indexPosition
> height
) {
220 if (android
.os
.Build
.VERSION
.SDK_INT
>= 11)
222 mList
.smoothScrollToPosition(index
);
224 else if (android
.os
.Build
.VERSION
.SDK_INT
>= 8)
226 mList
.setSelectionFromTop(index
, 0);
234 * Save index and top position
236 private void saveIndexAndTopPosition(int index
) {
240 int firstPosition
= mList
.getFirstVisiblePosition();
241 mFirstPositions
.add(firstPosition
);
243 View view
= mList
.getChildAt(0);
244 int top
= (view
== null
) ?
0 : view
.getTop() ;
248 // Save the height of a cell
249 mHeightCell
= (view
== null
|| mHeightCell
!= 0) ? mHeightCell
: view
.getHeight();
253 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
254 OCFile file
= mContainerActivity
.getStorageManager().createFileInstance(
255 (Cursor
) 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 ((FileDisplayActivity
)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
= mContainerActivity
.getStorageManager().createFileInstance(
301 (Cursor
) mAdapter
.getItem(info
.position
));
302 List
<Integer
> toHide
= new ArrayList
<Integer
>();
303 List
<Integer
> toDisable
= new ArrayList
<Integer
>();
305 MenuItem item
= null
;
306 if (targetFile
.isFolder()) {
307 // contextual menu for folders
308 toHide
.add(R
.id
.action_open_file_with
);
309 toHide
.add(R
.id
.action_download_file
);
310 toHide
.add(R
.id
.action_cancel_download
);
311 toHide
.add(R
.id
.action_cancel_upload
);
312 toHide
.add(R
.id
.action_sync_file
);
313 toHide
.add(R
.id
.action_see_details
);
314 toHide
.add(R
.id
.action_send_file
);
315 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile
) ||
316 mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile
) ) {
317 toDisable
.add(R
.id
.action_rename_file
);
318 toDisable
.add(R
.id
.action_remove_file
);
323 // contextual menu for regular files
325 // new design: 'download' and 'open with' won't be available anymore in context menu
326 toHide
.add(R
.id
.action_download_file
);
327 toHide
.add(R
.id
.action_open_file_with
);
329 if (targetFile
.isDown()) {
330 toHide
.add(R
.id
.action_cancel_download
);
331 toHide
.add(R
.id
.action_cancel_upload
);
334 toHide
.add(R
.id
.action_sync_file
);
336 if ( mContainerActivity
.getFileDownloaderBinder().isDownloading(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile
)) {
337 toHide
.add(R
.id
.action_cancel_upload
);
338 toDisable
.add(R
.id
.action_rename_file
);
339 toDisable
.add(R
.id
.action_remove_file
);
341 } else if ( mContainerActivity
.getFileUploaderBinder().isUploading(AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile
)) {
342 toHide
.add(R
.id
.action_cancel_download
);
343 toDisable
.add(R
.id
.action_rename_file
);
344 toDisable
.add(R
.id
.action_remove_file
);
347 toHide
.add(R
.id
.action_cancel_download
);
348 toHide
.add(R
.id
.action_cancel_upload
);
353 if (!targetFile
.isShareByLink()) {
354 toHide
.add(R
.id
.action_unshare_file
);
358 boolean sendEnabled
= getString(R
.string
.send_files_to_other_apps
).equalsIgnoreCase("on");
360 toHide
.add(R
.id
.action_send_file
);
363 for (int i
: toHide
) {
364 item
= menu
.findItem(i
);
366 item
.setVisible(false
);
367 item
.setEnabled(false
);
371 for (int i
: toDisable
) {
372 item
= menu
.findItem(i
);
374 item
.setEnabled(false
);
384 public boolean onContextItemSelected (MenuItem item
) {
385 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
386 mTargetFile
= mContainerActivity
.getStorageManager().createFileInstance(
387 (Cursor
) mAdapter
.getItem(info
.position
));
388 switch (item
.getItemId()) {
389 case R
.id
.action_share_file
: {
390 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
393 case R
.id
.action_unshare_file
: {
394 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
397 case R
.id
.action_rename_file
: {
398 String fileName
= mTargetFile
.getFileName();
399 int extensionStart
= mTargetFile
.isFolder() ?
-1 : fileName
.lastIndexOf(".");
400 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: fileName
.length();
401 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), fileName
, 0, selectionEnd
, this);
402 dialog
.show(getFragmentManager(), EditNameDialog
.TAG
);
405 case R
.id
.action_remove_file
: {
406 int messageStringId
= R
.string
.confirmation_remove_alert
;
407 int posBtnStringId
= R
.string
.confirmation_remove_remote
;
408 int neuBtnStringId
= -1;
409 if (mTargetFile
.isFolder()) {
410 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
411 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
412 neuBtnStringId
= R
.string
.confirmation_remove_folder_local
;
413 } else if (mTargetFile
.isDown()) {
414 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
415 neuBtnStringId
= R
.string
.confirmation_remove_local
;
417 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
419 new String
[]{mTargetFile
.getFileName()},
422 R
.string
.common_cancel
);
423 confDialog
.setOnConfirmationListener(this);
424 confDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
427 case R
.id
.action_sync_file
: {
428 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
431 case R
.id
.action_cancel_download
: {
432 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
433 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
434 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(account
, mTargetFile
)) {
435 downloaderBinder
.cancel(account
, mTargetFile
);
437 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
441 case R
.id
.action_cancel_upload
: {
442 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
443 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getSherlockActivity());
444 if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(account
, mTargetFile
)) {
445 uploaderBinder
.cancel(account
, mTargetFile
);
447 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
451 case R
.id
.action_see_details
: {
452 mContainerActivity
.showDetails(mTargetFile
);
455 case R
.id
.action_send_file
: {
457 if (!mTargetFile
.isDown()) { // Download the file
458 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
459 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(mTargetFile
);
462 ((FileDisplayActivity
)mContainerActivity
).getFileOperationsHelper().sendDownloadedFile(mTargetFile
);
467 return super.onContextItemSelected(item
);
473 * Use this to query the {@link OCFile} that is currently
474 * being displayed by this fragment
475 * @return The currently viewed OCFile
477 public OCFile
getCurrentFile(){
482 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
484 public void listDirectory(){
489 * Lists the given directory on the view. When the input parameter is null,
490 * it will either refresh the last known directory. list the root
491 * if there never was a directory.
493 * @param directory File to be listed
495 public void listDirectory(OCFile directory
) {
496 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
497 if (storageManager
!= null
) {
499 // Check input parameters for null
500 if(directory
== null
){
504 directory
= storageManager
.getFileByPath("/");
505 if (directory
== null
) return; // no files, wait for sync
510 // If that's not a directory -> List its parent
511 if(!directory
.isFolder()){
512 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
513 directory
= storageManager
.getFileById(directory
.getParentId());
516 mAdapter
.swapDirectory(directory
, storageManager
);
517 if (mFile
== null
|| !mFile
.equals(directory
)) {
518 mList
.setSelectionFromTop(0, 0);
527 public void onDismiss(EditNameDialog dialog
) {
528 if (dialog
.getResult()) {
529 String newFilename
= dialog
.getNewFilename();
530 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
531 mContainerActivity
.getFileOperationsHelper().renameFile(mTargetFile
, newFilename
);
537 public void onConfirmation(String callerTag
) {
538 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
539 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
540 if (storageManager
.getFileById(mTargetFile
.getFileId()) != null
) {
541 mContainerActivity
.getFileOperationsHelper().removeFile(mTargetFile
, true
);
547 public void onNeutral(String callerTag
) {
548 mContainerActivity
.getStorageManager().removeFile(mTargetFile
, false
, true
); // TODO perform in background task / new thread
550 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
554 public void onCancel(String callerTag
) {
555 Log_OC
.d(TAG
, "REMOVAL CANCELED");