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
;
22 import android
.app
.Activity
;
23 import android
.content
.Intent
;
24 import android
.os
.Bundle
;
25 import android
.support
.v4
.widget
.SwipeRefreshLayout
;
26 import android
.view
.ContextMenu
;
27 import android
.view
.MenuInflater
;
28 import android
.view
.MenuItem
;
29 import android
.view
.View
;
30 import android
.widget
.AdapterView
;
31 import android
.widget
.AdapterView
.AdapterContextMenuInfo
;
33 import com
.owncloud
.android
.R
;
34 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
35 import com
.owncloud
.android
.datamodel
.OCFile
;
36 import com
.owncloud
.android
.files
.FileMenuFilter
;
37 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
38 import com
.owncloud
.android
.ui
.activity
.MoveActivity
;
39 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
40 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
.sortOrders
;
41 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
42 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
43 import com
.owncloud
.android
.ui
.dialog
.RenameFileDialogFragment
;
44 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
45 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
46 import com
.owncloud
.android
.utils
.Log_OC
;
49 * A Fragment that lists all files and folders in a given path.
51 * TODO refactorize to get rid of direct dependency on FileDisplayActivity
53 * @author Bartek Przybylski
55 * @author David A. Velasco
57 public class OCFileListFragment
extends ExtendedListFragment
{
59 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
61 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ?
62 OCFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
64 public final static String ARG_JUST_FOLDERS
= MY_PACKAGE
+ ".JUST_FOLDERS";
65 public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS
= MY_PACKAGE
+ ".ALLOW_CONTEXTUAL";
67 private static final String KEY_FILE
= MY_PACKAGE
+ ".extra.FILE";
69 private FileFragment
.ContainerActivity mContainerActivity
;
71 private OCFile mFile
= null
;
72 private FileListListAdapter mAdapter
;
74 private OCFile mTargetFile
;
81 public void onAttach(Activity activity
) {
82 super.onAttach(activity
);
83 Log_OC
.e(TAG
, "onAttach");
85 mContainerActivity
= (FileFragment
.ContainerActivity
) activity
;
87 } catch (ClassCastException e
) {
88 throw new ClassCastException(activity
.toString() + " must implement " +
89 FileFragment
.ContainerActivity
.class.getSimpleName());
92 setOnRefreshListener((SwipeRefreshLayout
.OnRefreshListener
) activity
);
94 } catch (ClassCastException e
) {
95 throw new ClassCastException(activity
.toString() + " must implement " +
96 SwipeRefreshLayout
.OnRefreshListener
.class.getSimpleName());
102 public void onDetach() {
103 setOnRefreshListener(null
);
104 mContainerActivity
= null
;
112 public void onActivityCreated(Bundle savedInstanceState
) {
113 super.onActivityCreated(savedInstanceState
);
114 Log_OC
.e(TAG
, "onActivityCreated() start");
116 if (savedInstanceState
!= null
) {
117 mFile
= savedInstanceState
.getParcelable(KEY_FILE
);
120 Bundle args
= getArguments();
121 boolean justFolders
= (args
== null
) ? false
: args
.getBoolean(ARG_JUST_FOLDERS
, false
);
122 mAdapter
= new FileListListAdapter(
124 getSherlockActivity(),
127 setListAdapter(mAdapter
);
129 registerForContextMenu(getListView());
130 getListView().setOnCreateContextMenuListener(this);
134 * Saves the current listed folder.
137 public void onSaveInstanceState (Bundle outState
) {
138 super.onSaveInstanceState(outState
);
139 outState
.putParcelable(KEY_FILE
, mFile
);
143 * Call this, when the user presses the up button.
145 * Tries to move up the current folder one level. If the parent folder was removed from the
146 * database, it continues browsing up until finding an existing folders.
148 * return Count of folder levels browsed up.
150 public int onBrowseUp() {
151 OCFile parentDir
= null
;
155 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
157 String parentPath
= null
;
158 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
159 parentPath
= new File(mFile
.getRemotePath()).getParent();
160 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
161 parentPath
+ OCFile
.PATH_SEPARATOR
;
162 parentDir
= storageManager
.getFileByPath(parentPath
);
165 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
);
167 while (parentDir
== null
) {
168 parentPath
= new File(parentPath
).getParent();
169 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
170 parentPath
+ OCFile
.PATH_SEPARATOR
;
171 parentDir
= storageManager
.getFileByPath(parentPath
);
173 } // exit is granted because storageManager.getFileByPath("/") never returns null
176 listDirectory(mFile
);
180 // restore index and top position
181 restoreIndexAndTopPosition();
183 } // else - should never happen now
189 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
190 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
192 if (file
.isFolder()) {
193 // update state and view of this fragment
195 // then, notify parent activity to let it update its state and view
196 mContainerActivity
.onBrowsedDownTo(file
);
197 // save index and top position
198 saveIndexAndTopPosition(position
);
200 } else { /// Click on a file
201 if (PreviewImageFragment
.canBePreviewed(file
)) {
202 // preview image - it handles the download, if needed
203 ((FileDisplayActivity
)mContainerActivity
).startImagePreview(file
);
205 } else if (file
.isDown()) {
206 if (PreviewMediaFragment
.canBePreviewed(file
)) {
208 ((FileDisplayActivity
)mContainerActivity
).startMediaPreview(file
, 0, true
);
210 mContainerActivity
.getFileOperationsHelper().openFile(file
);
214 // automatic download, preview on finish
215 ((FileDisplayActivity
)mContainerActivity
).startDownloadForPreview(file
);
221 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
230 public void onCreateContextMenu (
231 ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
232 super.onCreateContextMenu(menu
, v
, menuInfo
);
233 Bundle args
= getArguments();
234 boolean allowContextualActions
=
235 (args
== null
) ? true
: args
.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS
, true
);
236 if (allowContextualActions
) {
237 MenuInflater inflater
= getSherlockActivity().getMenuInflater();
238 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
239 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
240 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
242 if (mContainerActivity
.getStorageManager() != null
) {
243 FileMenuFilter mf
= new FileMenuFilter(
245 mContainerActivity
.getStorageManager().getAccount(),
247 getSherlockActivity()
252 /// additional restrictions for this fragment
253 // TODO allow in the future 'open with' for previewable files
254 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
256 item
.setVisible(false
);
257 item
.setEnabled(false
);
259 /// TODO break this direct dependency on FileDisplayActivity... if possible
260 FileFragment frag
= ((FileDisplayActivity
)getSherlockActivity()).getSecondFragment();
261 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
262 frag
.getFile().getFileId() == targetFile
.getFileId()) {
263 item
= menu
.findItem(R
.id
.action_see_details
);
265 item
.setVisible(false
);
266 item
.setEnabled(false
);
277 public boolean onContextItemSelected (MenuItem item
) {
278 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
279 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
280 switch (item
.getItemId()) {
281 case R
.id
.action_share_file
: {
282 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
285 case R
.id
.action_unshare_file
: {
286 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
289 case R
.id
.action_rename_file
: {
290 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(mTargetFile
);
291 dialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_RENAME_FILE
);
294 case R
.id
.action_remove_file
: {
295 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(mTargetFile
);
296 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
299 case R
.id
.action_download_file
:
300 case R
.id
.action_sync_file
: {
301 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
304 case R
.id
.action_cancel_download
:
305 case R
.id
.action_cancel_upload
: {
306 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(mTargetFile
);
309 case R
.id
.action_see_details
: {
310 mContainerActivity
.showDetails(mTargetFile
);
313 case R
.id
.action_send_file
: {
315 if (!mTargetFile
.isDown()) { // Download the file
316 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
317 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(mTargetFile
);
320 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(mTargetFile
);
324 case R
.id
.action_move
: {
325 Intent action
= new Intent(getActivity(), MoveActivity
.class);
327 // Pass mTargetFile that contains info of selected file/folder
328 action
.putExtra(MoveActivity
.EXTRA_TARGET_FILE
, mTargetFile
);
329 getActivity().startActivityForResult(action
, FileDisplayActivity
.ACTION_MOVE_FILES
);
333 return super.onContextItemSelected(item
);
339 * Use this to query the {@link OCFile} that is currently
340 * being displayed by this fragment
341 * @return The currently viewed OCFile
343 public OCFile
getCurrentFile(){
348 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
350 public void listDirectory(){
355 * Lists the given directory on the view. When the input parameter is null,
356 * it will either refresh the last known directory. list the root
357 * if there never was a directory.
359 * @param directory File to be listed
361 public void listDirectory(OCFile directory
) {
362 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
363 if (storageManager
!= null
) {
365 // Check input parameters for null
366 if(directory
== null
){
370 directory
= storageManager
.getFileByPath("/");
371 if (directory
== null
) return; // no files, wait for sync
376 // If that's not a directory -> List its parent
377 if(!directory
.isFolder()){
378 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
379 directory
= storageManager
.getFileById(directory
.getParentId());
382 mAdapter
.swapDirectory(directory
, storageManager
);
383 if (mFile
== null
|| !mFile
.equals(directory
)) {
384 mList
.setSelectionFromTop(0, 0);
390 public void sortByDate(boolean descending
){
391 mAdapter
.setSortOrder(sortOrders
.DATE
, descending
);
394 public void sortBySize(boolean descending
){
395 mAdapter
.setSortOrder(sortOrders
.SIZE
, descending
);
398 public void sortByName(boolean descending
){
399 mAdapter
.setSortOrder(sortOrders
.NAME
, descending
);