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
.datamodel
.FileDataStorageManager
;
25 import com
.owncloud
.android
.datamodel
.OCFile
;
26 import com
.owncloud
.android
.files
.FileMenuFilter
;
27 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
28 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
29 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
30 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
31 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
;
32 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
33 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
34 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
35 import com
.owncloud
.android
.utils
.Log_OC
;
37 import android
.app
.Activity
;
38 import android
.os
.Bundle
;
39 import android
.view
.ContextMenu
;
40 import android
.view
.MenuInflater
;
41 import android
.view
.MenuItem
;
42 import android
.view
.View
;
43 import android
.widget
.AdapterView
;
44 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
47 * A Fragment that lists all files and folders in a given path.
49 * TODO refactorize to get rid of direct dependency on FileDisplayActivity
51 * @author Bartek Przybylski
53 * @author David A. Velasco
55 public class OCFileListFragment
extends ExtendedListFragment
56 implements EditNameDialogListener
, ConfirmationDialogFragmentListener
{
58 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
60 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ? OCFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
61 private static final String EXTRA_FILE
= MY_PACKAGE
+ ".extra.FILE";
63 private static final String KEY_INDEXES
= "INDEXES";
64 private static final String KEY_FIRST_POSITIONS
= "FIRST_POSITIONS";
65 private static final String KEY_TOPS
= "TOPS";
66 private static final String KEY_HEIGHT_CELL
= "HEIGHT_CELL";
68 private FileFragment
.ContainerActivity mContainerActivity
;
70 private OCFile mFile
= null
;
71 private FileListListAdapter mAdapter
;
73 private OCFile mTargetFile
;
75 // Save the state of the scroll in browsing
76 private ArrayList
<Integer
> mIndexes
;
77 private ArrayList
<Integer
> mFirstPositions
;
78 private ArrayList
<Integer
> mTops
;
80 private int mHeightCell
= 0;
86 public void onAttach(Activity activity
) {
87 super.onAttach(activity
);
88 Log_OC
.e(TAG
, "onAttach");
90 mContainerActivity
= (FileFragment
.ContainerActivity
) activity
;
91 } catch (ClassCastException e
) {
92 throw new ClassCastException(activity
.toString() + " must implement " +
93 FileFragment
.ContainerActivity
.class.getSimpleName());
99 public void onDetach() {
100 mContainerActivity
= null
;
108 public void onActivityCreated(Bundle savedInstanceState
) {
109 super.onActivityCreated(savedInstanceState
);
110 Log_OC
.e(TAG
, "onActivityCreated() start");
111 mAdapter
= new FileListListAdapter(getSherlockActivity(), mContainerActivity
);
113 if (savedInstanceState
!= null
) {
114 mFile
= savedInstanceState
.getParcelable(EXTRA_FILE
);
115 mIndexes
= savedInstanceState
.getIntegerArrayList(KEY_INDEXES
);
116 mFirstPositions
= savedInstanceState
.getIntegerArrayList(KEY_FIRST_POSITIONS
);
117 mTops
= savedInstanceState
.getIntegerArrayList(KEY_TOPS
);
118 mHeightCell
= savedInstanceState
.getInt(KEY_HEIGHT_CELL
);
121 mIndexes
= new ArrayList
<Integer
>();
122 mFirstPositions
= new ArrayList
<Integer
>();
123 mTops
= new ArrayList
<Integer
>();
128 mAdapter
= new FileListListAdapter(getSherlockActivity(), mContainerActivity
);
130 setListAdapter(mAdapter
);
132 registerForContextMenu(getListView());
133 getListView().setOnCreateContextMenuListener(this);
135 // mHandler = new Handler();
139 * Saves the current listed folder.
142 public void onSaveInstanceState (Bundle outState
) {
143 super.onSaveInstanceState(outState
);
144 outState
.putParcelable(EXTRA_FILE
, mFile
);
145 outState
.putIntegerArrayList(KEY_INDEXES
, mIndexes
);
146 outState
.putIntegerArrayList(KEY_FIRST_POSITIONS
, mFirstPositions
);
147 outState
.putIntegerArrayList(KEY_TOPS
, mTops
);
148 outState
.putInt(KEY_HEIGHT_CELL
, mHeightCell
);
152 * Call this, when the user presses the up button.
154 * Tries to move up the current folder one level. If the parent folder was removed from the database,
155 * it continues browsing up until finding an existing folders.
157 * return Count of folder levels browsed up.
159 public int onBrowseUp() {
160 OCFile parentDir
= null
;
164 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
166 String parentPath
= null
;
167 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
168 parentPath
= new File(mFile
.getRemotePath()).getParent();
169 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
170 parentDir
= storageManager
.getFileByPath(parentPath
);
173 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
); // never returns null; keep the path in root folder
175 while (parentDir
== null
) {
176 parentPath
= new File(parentPath
).getParent();
177 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
178 parentDir
= storageManager
.getFileByPath(parentPath
);
180 } // exit is granted because storageManager.getFileByPath("/") never returns null
185 listDirectory(mFile
);
187 ((FileDisplayActivity
)mContainerActivity
).startSyncFolderOperation(mFile
);
189 // restore index and top position
190 restoreIndexAndTopPosition();
192 } // else - should never happen now
198 * Restore index and position
200 private void restoreIndexAndTopPosition() {
201 if (mIndexes
.size() > 0) {
202 // needs to be checked; not every browse-up had a browse-down before
204 int index
= mIndexes
.remove(mIndexes
.size() - 1);
206 int firstPosition
= mFirstPositions
.remove(mFirstPositions
.size() -1);
208 int top
= mTops
.remove(mTops
.size() - 1);
210 mList
.setSelectionFromTop(firstPosition
, top
);
212 // Move the scroll if the selection is not visible
213 int indexPosition
= mHeightCell
*index
;
214 int height
= mList
.getHeight();
216 if (indexPosition
> height
) {
217 if (android
.os
.Build
.VERSION
.SDK_INT
>= 11)
219 mList
.smoothScrollToPosition(index
);
221 else if (android
.os
.Build
.VERSION
.SDK_INT
>= 8)
223 mList
.setSelectionFromTop(index
, 0);
231 * Save index and top position
233 private void saveIndexAndTopPosition(int index
) {
237 int firstPosition
= mList
.getFirstVisiblePosition();
238 mFirstPositions
.add(firstPosition
);
240 View view
= mList
.getChildAt(0);
241 int top
= (view
== null
) ?
0 : view
.getTop() ;
245 // Save the height of a cell
246 mHeightCell
= (view
== null
|| mHeightCell
!= 0) ? mHeightCell
: view
.getHeight();
250 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
251 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
253 if (file
.isFolder()) {
254 // update state and view of this fragment
256 // then, notify parent activity to let it update its state and view, and other fragments
257 mContainerActivity
.onBrowsedDownTo(file
);
258 // save index and top position
259 saveIndexAndTopPosition(position
);
261 } else { /// Click on a file
262 if (PreviewImageFragment
.canBePreviewed(file
)) {
263 // preview image - it handles the download, if needed
264 ((FileDisplayActivity
)mContainerActivity
).startImagePreview(file
);
266 } else if (file
.isDown()) {
267 if (PreviewMediaFragment
.canBePreviewed(file
)) {
269 ((FileDisplayActivity
)mContainerActivity
).startMediaPreview(file
, 0, true
);
271 mContainerActivity
.getFileOperationsHelper().openFile(file
);
275 // automatic download, preview on finish
276 ((FileDisplayActivity
)mContainerActivity
).startDownloadForPreview(file
);
282 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
291 public void onCreateContextMenu (ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
292 super.onCreateContextMenu(menu
, v
, menuInfo
);
293 MenuInflater inflater
= getSherlockActivity().getMenuInflater();
294 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
295 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
296 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
298 FileMenuFilter mf
= new FileMenuFilter(
300 mContainerActivity
.getStorageManager().getAccount(),
302 getSherlockActivity()
306 /// additional restrictions for this fragment
307 // TODO allow in the future 'open with' for previewable files
308 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
310 item
.setVisible(false
);
311 item
.setEnabled(false
);
313 /// TODO break this direct dependency on FileDisplayActivity... if possible
314 FileFragment frag
= ((FileDisplayActivity
)getSherlockActivity()).getSecondFragment();
315 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
316 frag
.getFile().getFileId() == targetFile
.getFileId()) {
317 item
= menu
.findItem(R
.id
.action_see_details
);
319 item
.setVisible(false
);
320 item
.setEnabled(false
);
332 public boolean onContextItemSelected (MenuItem item
) {
333 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
334 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
335 switch (item
.getItemId()) {
336 case R
.id
.action_share_file
: {
337 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
340 case R
.id
.action_unshare_file
: {
341 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
344 case R
.id
.action_rename_file
: {
345 String fileName
= mTargetFile
.getFileName();
346 int extensionStart
= mTargetFile
.isFolder() ?
-1 : fileName
.lastIndexOf(".");
347 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: fileName
.length();
348 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), fileName
, 0, selectionEnd
, this);
349 dialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_RENAME_FILE
);
352 case R
.id
.action_remove_file
: {
353 int messageStringId
= R
.string
.confirmation_remove_alert
;
354 int posBtnStringId
= R
.string
.confirmation_remove_remote
;
355 int neuBtnStringId
= -1;
356 if (mTargetFile
.isFolder()) {
357 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
358 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
359 neuBtnStringId
= R
.string
.confirmation_remove_folder_local
;
360 } else if (mTargetFile
.isDown()) {
361 posBtnStringId
= R
.string
.confirmation_remove_remote_and_local
;
362 neuBtnStringId
= R
.string
.confirmation_remove_local
;
364 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
366 new String
[]{mTargetFile
.getFileName()},
369 R
.string
.common_cancel
);
370 confDialog
.setOnConfirmationListener(this);
371 confDialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_CONFIRMATION
);
374 case R
.id
.action_download_file
:
375 case R
.id
.action_sync_file
: {
376 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
379 case R
.id
.action_cancel_download
:
380 case R
.id
.action_cancel_upload
: {
381 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(mTargetFile
);
384 case R
.id
.action_see_details
: {
385 mContainerActivity
.showDetails(mTargetFile
);
388 case R
.id
.action_send_file
: {
390 if (!mTargetFile
.isDown()) { // Download the file
391 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
392 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(mTargetFile
);
395 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(mTargetFile
);
400 return super.onContextItemSelected(item
);
406 * Use this to query the {@link OCFile} that is currently
407 * being displayed by this fragment
408 * @return The currently viewed OCFile
410 public OCFile
getCurrentFile(){
415 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
417 public void listDirectory(){
422 * Lists the given directory on the view. When the input parameter is null,
423 * it will either refresh the last known directory. list the root
424 * if there never was a directory.
426 * @param directory File to be listed
428 public void listDirectory(OCFile directory
) {
429 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
430 if (storageManager
!= null
) {
432 // Check input parameters for null
433 if(directory
== null
){
437 directory
= storageManager
.getFileByPath("/");
438 if (directory
== null
) return; // no files, wait for sync
443 // If that's not a directory -> List its parent
444 if(!directory
.isFolder()){
445 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
446 directory
= storageManager
.getFileById(directory
.getParentId());
449 mAdapter
.swapDirectory(directory
, storageManager
);
450 if (mFile
== null
|| !mFile
.equals(directory
)) {
451 mList
.setSelectionFromTop(0, 0);
460 public void onDismiss(EditNameDialog dialog
) {
461 if (dialog
.getResult()) {
462 String newFilename
= dialog
.getNewFilename();
463 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
464 mContainerActivity
.getFileOperationsHelper().renameFile(mTargetFile
, newFilename
);
470 public void onConfirmation(String callerTag
) {
471 if (callerTag
.equals(FileDetailFragment
.FTAG_CONFIRMATION
)) {
472 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
473 if (storageManager
.getFileById(mTargetFile
.getFileId()) != null
) {
474 mContainerActivity
.getFileOperationsHelper().removeFile(mTargetFile
, true
);
480 public void onNeutral(String callerTag
) {
481 mContainerActivity
.getStorageManager().removeFile(mTargetFile
, false
, true
); // TODO perform in background task / new thread
483 mContainerActivity
.onTransferStateChanged(mTargetFile
, false
, false
);
487 public void onCancel(String callerTag
) {
488 Log_OC
.d(TAG
, "REMOVAL CANCELED");