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
.dialog
.ConfirmationDialogFragment
;
41 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
42 import com
.owncloud
.android
.ui
.dialog
.RenameFileDialogFragment
;
43 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
44 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
45 import com
.owncloud
.android
.utils
.Log_OC
;
48 * A Fragment that lists all files and folders in a given path.
50 * TODO refactorize to get rid of direct dependency on FileDisplayActivity
52 * @author Bartek Przybylski
54 * @author David A. Velasco
56 public class OCFileListFragment
extends ExtendedListFragment
{
58 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
60 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ? OCFileListFragment
.class
61 .getPackage().getName() : "com.owncloud.android.ui.fragment";
63 public final static String ARG_JUST_FOLDERS
= MY_PACKAGE
+ ".JUST_FOLDERS";
64 public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS
= MY_PACKAGE
+ ".ALLOW_CONTEXTUAL";
66 private static final String KEY_FILE
= MY_PACKAGE
+ ".extra.FILE";
68 private FileFragment
.ContainerActivity mContainerActivity
;
70 private OCFile mFile
= null
;
71 private FileListListAdapter mAdapter
;
73 private OCFile mTargetFile
;
79 public void onAttach(Activity activity
) {
80 super.onAttach(activity
);
81 Log_OC
.e(TAG
, "onAttach");
83 mContainerActivity
= (FileFragment
.ContainerActivity
) activity
;
85 } catch (ClassCastException e
) {
86 throw new ClassCastException(activity
.toString() + " must implement "
87 + FileFragment
.ContainerActivity
.class.getSimpleName());
90 setOnRefreshListener((SwipeRefreshLayout
.OnRefreshListener
) activity
);
92 } catch (ClassCastException e
) {
93 throw new ClassCastException(activity
.toString() + " must implement "
94 + SwipeRefreshLayout
.OnRefreshListener
.class.getSimpleName());
99 public void onDetach() {
100 setOnRefreshListener(null
);
101 mContainerActivity
= null
;
109 public void onActivityCreated(Bundle savedInstanceState
) {
110 super.onActivityCreated(savedInstanceState
);
111 Log_OC
.e(TAG
, "onActivityCreated() start");
113 if (savedInstanceState
!= null
) {
114 mFile
= savedInstanceState
.getParcelable(KEY_FILE
);
117 Bundle args
= getArguments();
118 boolean justFolders
= (args
== null
) ? false
: args
.getBoolean(ARG_JUST_FOLDERS
, false
);
119 mAdapter
= new FileListListAdapter(justFolders
, getSherlockActivity(), mContainerActivity
);
120 setListAdapter(mAdapter
);
122 registerForContextMenu(getListView());
123 getListView().setOnCreateContextMenuListener(this);
127 * Saves the current listed folder.
130 public void onSaveInstanceState(Bundle outState
) {
131 super.onSaveInstanceState(outState
);
132 outState
.putParcelable(KEY_FILE
, mFile
);
136 * Call this, when the user presses the up button.
138 * Tries to move up the current folder one level. If the parent folder was
139 * removed from the database, it continues browsing up until finding an
142 * return Count of folder levels browsed up.
144 public int onBrowseUp() {
145 OCFile parentDir
= null
;
149 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
151 String parentPath
= null
;
152 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
153 parentPath
= new File(mFile
.getRemotePath()).getParent();
154 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
155 + OCFile
.PATH_SEPARATOR
;
156 parentDir
= storageManager
.getFileByPath(parentPath
);
159 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
);
161 while (parentDir
== null
) {
162 parentPath
= new File(parentPath
).getParent();
163 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
164 + OCFile
.PATH_SEPARATOR
;
165 parentDir
= storageManager
.getFileByPath(parentPath
);
167 } // exit is granted because storageManager.getFileByPath("/") never
171 listDirectory(mFile
);
175 // restore index and top position
176 restoreIndexAndTopPosition();
178 } // else - should never happen now
184 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
185 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
187 if (file
.isFolder()) {
188 // update state and view of this fragment
190 // then, notify parent activity to let it update its state and
192 mContainerActivity
.onBrowsedDownTo(file
);
193 // save index and top position
194 saveIndexAndTopPosition(position
);
196 } else { // / Click on a file
197 if (PreviewImageFragment
.canBePreviewed(file
)) {
198 // preview image - it handles the download, if needed
199 ((FileDisplayActivity
) mContainerActivity
).startImagePreview(file
);
201 } else if (file
.isDown()) {
202 if (PreviewMediaFragment
.canBePreviewed(file
)) {
204 ((FileDisplayActivity
) mContainerActivity
).startMediaPreview(file
, 0, true
);
206 mContainerActivity
.getFileOperationsHelper().openFile(file
);
210 // automatic download, preview on finish
211 ((FileDisplayActivity
) mContainerActivity
).startDownloadForPreview(file
);
217 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
226 public void onCreateContextMenu(ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
227 super.onCreateContextMenu(menu
, v
, menuInfo
);
228 Bundle args
= getArguments();
229 boolean allowContextualActions
= (args
== null
) ? true
: args
.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS
, true
);
230 if (allowContextualActions
) {
231 MenuInflater inflater
= getSherlockActivity().getMenuInflater();
232 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
233 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
234 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
236 if (mContainerActivity
.getStorageManager() != null
) {
237 FileMenuFilter mf
= new FileMenuFilter(targetFile
, mContainerActivity
.getStorageManager()
238 .getAccount(), mContainerActivity
, getSherlockActivity());
242 // / additional restrictions for this fragment
243 // TODO allow in the future 'open with' for previewable files
244 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
246 item
.setVisible(false
);
247 item
.setEnabled(false
);
249 // / TODO break this direct dependency on FileDisplayActivity... if
251 FileFragment frag
= ((FileDisplayActivity
) getSherlockActivity()).getSecondFragment();
252 if (frag
!= null
&& frag
instanceof FileDetailFragment
253 && frag
.getFile().getFileId() == targetFile
.getFileId()) {
254 item
= menu
.findItem(R
.id
.action_see_details
);
256 item
.setVisible(false
);
257 item
.setEnabled(false
);
267 public boolean onContextItemSelected(MenuItem item
) {
268 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
269 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
270 switch (item
.getItemId()) {
271 case R
.id
.action_share_file
: {
272 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
275 case R
.id
.action_unshare_file
: {
276 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
279 case R
.id
.action_rename_file
: {
280 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(mTargetFile
);
281 dialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_RENAME_FILE
);
284 case R
.id
.action_remove_file
: {
285 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(mTargetFile
);
286 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
289 case R
.id
.action_download_file
:
290 case R
.id
.action_sync_file
: {
291 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
294 case R
.id
.action_cancel_download
:
295 case R
.id
.action_cancel_upload
: {
296 ((FileDisplayActivity
) mContainerActivity
).cancelTransference(mTargetFile
);
299 case R
.id
.action_see_details
: {
300 mContainerActivity
.showDetails(mTargetFile
);
303 case R
.id
.action_send_file
: {
305 if (!mTargetFile
.isDown()) { // Download the file
306 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
307 ((FileDisplayActivity
) mContainerActivity
).startDownloadForSending(mTargetFile
);
310 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(mTargetFile
);
314 case R
.id
.action_move
: {
315 Intent action
= new Intent(getActivity(), MoveActivity
.class);
317 // Pass mTargetFile that contains info of selected file/folder
318 action
.putExtra(MoveActivity
.EXTRA_TARGET_FILE
, mTargetFile
);
319 getActivity().startActivityForResult(action
, FileDisplayActivity
.ACTION_MOVE_FILES
);
323 return super.onContextItemSelected(item
);
328 * Use this to query the {@link OCFile} that is currently being displayed by
331 * @return The currently viewed OCFile
333 public OCFile
getCurrentFile() {
338 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null
341 public void listDirectory() {
346 * Lists the given directory on the view. When the input parameter is null,
347 * it will either refresh the last known directory. list the root if there
348 * never was a directory.
353 public void listDirectory(OCFile directory
) {
354 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
355 if (storageManager
!= null
) {
357 // Check input parameters for null
358 if (directory
== null
) {
362 directory
= storageManager
.getFileByPath("/");
363 if (directory
== null
)
364 return; // no files, wait for sync
368 // If that's not a directory -> List its parent
369 if (!directory
.isFolder()) {
370 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
371 directory
= storageManager
.getFileById(directory
.getParentId());
374 mAdapter
.swapDirectory(directory
, storageManager
);
375 if (mFile
== null
|| !mFile
.equals(directory
)) {
376 mList
.setSelectionFromTop(0, 0);
382 public void sortByName(boolean descending
) {
383 mAdapter
.setSortOrder(FileListListAdapter
.mSortName
, descending
);
386 public void sortByDate(boolean descending
) {
387 mAdapter
.setSortOrder(FileListListAdapter
.mSortDate
, descending
);
390 public void sortBySize(boolean descending
) {
391 mAdapter
.setSortOrder(FileListListAdapter
.mSortSize
, descending
);