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 android
.app
.Activity
;
24 import android
.os
.Bundle
;
25 import android
.view
.ContextMenu
;
26 import android
.view
.MenuInflater
;
27 import android
.view
.MenuItem
;
28 import android
.view
.View
;
29 import android
.widget
.AdapterView
;
30 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
32 import com
.owncloud
.android
.R
;
33 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
34 import com
.owncloud
.android
.datamodel
.OCFile
;
35 import com
.owncloud
.android
.files
.FileMenuFilter
;
36 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
37 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
38 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
39 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
40 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
41 import com
.owncloud
.android
.ui
.dialog
.RenameFileDialogFragment
;
42 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
43 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
46 * A Fragment that lists all files and folders in a given path.
48 * TODO refactorize to get rid of direct dependency on FileDisplayActivity
50 * @author Bartek Przybylski
52 * @author David A. Velasco
54 public class OCFileListFragment
extends ExtendedListFragment
{
56 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
58 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ? OCFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
59 private static final String EXTRA_FILE
= MY_PACKAGE
+ ".extra.FILE";
61 private static final String KEY_INDEXES
= "INDEXES";
62 private static final String KEY_FIRST_POSITIONS
= "FIRST_POSITIONS";
63 private static final String KEY_TOPS
= "TOPS";
64 private static final String KEY_HEIGHT_CELL
= "HEIGHT_CELL";
65 private static final String KEY_EMPTY_LIST_MESSAGE
= "EMPTY_LIST_MESSAGE";
67 private FileFragment
.ContainerActivity mContainerActivity
;
69 private OCFile mFile
= null
;
70 private FileListListAdapter mAdapter
;
72 private OCFile mTargetFile
;
74 // Save the state of the scroll in browsing
75 private ArrayList
<Integer
> mIndexes
;
76 private ArrayList
<Integer
> mFirstPositions
;
77 private ArrayList
<Integer
> mTops
;
79 private int mHeightCell
= 0;
85 public void onAttach(Activity activity
) {
86 super.onAttach(activity
);
87 Log_OC
.e(TAG
, "onAttach");
89 mContainerActivity
= (FileFragment
.ContainerActivity
) activity
;
90 } catch (ClassCastException e
) {
91 throw new ClassCastException(activity
.toString() + " must implement " +
92 FileFragment
.ContainerActivity
.class.getSimpleName());
98 public void onDetach() {
99 mContainerActivity
= null
;
107 public void onActivityCreated(Bundle savedInstanceState
) {
108 super.onActivityCreated(savedInstanceState
);
109 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
);
119 setMessageForEmptyList(savedInstanceState
.getString(KEY_EMPTY_LIST_MESSAGE
));
122 mIndexes
= new ArrayList
<Integer
>();
123 mFirstPositions
= new ArrayList
<Integer
>();
124 mTops
= new ArrayList
<Integer
>();
129 mAdapter
= new FileListListAdapter(getSherlockActivity(), mContainerActivity
);
131 setListAdapter(mAdapter
);
133 registerForContextMenu(getListView());
134 getListView().setOnCreateContextMenuListener(this);
138 * Saves the current listed folder.
141 public void onSaveInstanceState (Bundle outState
) {
142 super.onSaveInstanceState(outState
);
143 outState
.putParcelable(EXTRA_FILE
, mFile
);
144 outState
.putIntegerArrayList(KEY_INDEXES
, mIndexes
);
145 outState
.putIntegerArrayList(KEY_FIRST_POSITIONS
, mFirstPositions
);
146 outState
.putIntegerArrayList(KEY_TOPS
, mTops
);
147 outState
.putInt(KEY_HEIGHT_CELL
, mHeightCell
);
148 outState
.putString(KEY_EMPTY_LIST_MESSAGE
, getEmptyViewText());
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 if (mContainerActivity
.getStorageManager() != null
) {
299 FileMenuFilter mf
= new FileMenuFilter(
301 mContainerActivity
.getStorageManager().getAccount(),
303 getSherlockActivity()
308 /// additional restrictions for this fragment
309 // TODO allow in the future 'open with' for previewable files
310 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
312 item
.setVisible(false
);
313 item
.setEnabled(false
);
315 /// TODO break this direct dependency on FileDisplayActivity... if possible
316 FileFragment frag
= ((FileDisplayActivity
)getSherlockActivity()).getSecondFragment();
317 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
318 frag
.getFile().getFileId() == targetFile
.getFileId()) {
319 item
= menu
.findItem(R
.id
.action_see_details
);
321 item
.setVisible(false
);
322 item
.setEnabled(false
);
334 public boolean onContextItemSelected (MenuItem item
) {
335 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
336 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
337 switch (item
.getItemId()) {
338 case R
.id
.action_share_file
: {
339 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
342 case R
.id
.action_unshare_file
: {
343 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
346 case R
.id
.action_rename_file
: {
347 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(mTargetFile
);
348 dialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_RENAME_FILE
);
351 case R
.id
.action_remove_file
: {
352 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(mTargetFile
);
353 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
356 case R
.id
.action_download_file
:
357 case R
.id
.action_sync_file
: {
358 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
361 case R
.id
.action_cancel_download
:
362 case R
.id
.action_cancel_upload
: {
363 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(mTargetFile
);
366 case R
.id
.action_see_details
: {
367 mContainerActivity
.showDetails(mTargetFile
);
370 case R
.id
.action_send_file
: {
372 if (!mTargetFile
.isDown()) { // Download the file
373 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
374 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(mTargetFile
);
377 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(mTargetFile
);
382 return super.onContextItemSelected(item
);
388 * Use this to query the {@link OCFile} that is currently
389 * being displayed by this fragment
390 * @return The currently viewed OCFile
392 public OCFile
getCurrentFile(){
397 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
399 public void listDirectory(){
404 * Lists the given directory on the view. When the input parameter is null,
405 * it will either refresh the last known directory. list the root
406 * if there never was a directory.
408 * @param directory File to be listed
410 public void listDirectory(OCFile directory
) {
411 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
412 if (storageManager
!= null
) {
414 // Check input parameters for null
415 if(directory
== null
){
419 directory
= storageManager
.getFileByPath("/");
420 if (directory
== null
) return; // no files, wait for sync
425 // If that's not a directory -> List its parent
426 if(!directory
.isFolder()){
427 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
428 directory
= storageManager
.getFileById(directory
.getParentId());
431 mAdapter
.swapDirectory(directory
, storageManager
);
432 if (mFile
== null
|| !mFile
.equals(directory
)) {
433 mList
.setSelectionFromTop(0, 0);
441 public void onRefresh() {
446 mFile
= mContainerActivity
.getStorageManager().getFileById(mFile
.getFileId());
448 listDirectory(mFile
);
450 ((FileDisplayActivity
)mContainerActivity
).startSyncFolderOperation(mFile
);