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
.lib
.common
.utils
.Log_OC
;
38 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
39 import com
.owncloud
.android
.ui
.activity
.MoveActivity
;
40 import com
.owncloud
.android
.ui
.activity
.OnEnforceableRefreshListener
;
41 import com
.owncloud
.android
.ui
.adapter
.FileListListAdapter
;
42 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
43 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
44 import com
.owncloud
.android
.ui
.dialog
.RenameFileDialogFragment
;
45 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
46 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
47 import com
.owncloud
.android
.ui
.preview
.PreviewTextFragment
;
50 * A Fragment that lists all files and folders in a given path.
52 * TODO refactorize to get rid of direct dependency on FileDisplayActivity
54 * @author Bartek Przybylski
56 * @author David A. Velasco
58 public class OCFileListFragment
extends ExtendedListFragment
{
60 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
62 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ?
63 OCFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
65 public final static String ARG_JUST_FOLDERS
= MY_PACKAGE
+ ".JUST_FOLDERS";
66 public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS
= MY_PACKAGE
+ ".ALLOW_CONTEXTUAL";
68 private static final String KEY_FILE
= MY_PACKAGE
+ ".extra.FILE";
70 private FileFragment
.ContainerActivity mContainerActivity
;
72 private OCFile mFile
= null
;
73 private FileListListAdapter mAdapter
;
75 private OCFile mTargetFile
;
82 public void onAttach(Activity activity
) {
83 super.onAttach(activity
);
84 Log_OC
.e(TAG
, "onAttach");
86 mContainerActivity
= (FileFragment
.ContainerActivity
) activity
;
88 } catch (ClassCastException e
) {
89 throw new ClassCastException(activity
.toString() + " must implement " +
90 FileFragment
.ContainerActivity
.class.getSimpleName());
93 setOnRefreshListener((OnEnforceableRefreshListener
) activity
);
95 } catch (ClassCastException e
) {
96 throw new ClassCastException(activity
.toString() + " must implement " +
97 SwipeRefreshLayout
.OnRefreshListener
.class.getSimpleName());
103 public void onDetach() {
104 setOnRefreshListener(null
);
105 mContainerActivity
= null
;
113 public void onActivityCreated(Bundle savedInstanceState
) {
114 super.onActivityCreated(savedInstanceState
);
115 Log_OC
.e(TAG
, "onActivityCreated() start");
117 if (savedInstanceState
!= null
) {
118 mFile
= savedInstanceState
.getParcelable(KEY_FILE
);
121 Bundle args
= getArguments();
122 boolean justFolders
= (args
== null
) ? false
: args
.getBoolean(ARG_JUST_FOLDERS
, false
);
123 mAdapter
= new FileListListAdapter(
125 getSherlockActivity(),
128 setListAdapter(mAdapter
);
130 registerForContextMenu(getListView());
131 getListView().setOnCreateContextMenuListener(this);
135 * Saves the current listed folder.
138 public void onSaveInstanceState (Bundle outState
) {
139 super.onSaveInstanceState(outState
);
140 outState
.putParcelable(KEY_FILE
, mFile
);
144 * Call this, when the user presses the up button.
146 * Tries to move up the current folder one level. If the parent folder was removed from the
147 * database, it continues browsing up until finding an existing folders.
149 * return Count of folder levels browsed up.
151 public int onBrowseUp() {
152 OCFile parentDir
= null
;
156 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
158 String parentPath
= null
;
159 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
160 parentPath
= new File(mFile
.getRemotePath()).getParent();
161 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
162 parentPath
+ OCFile
.PATH_SEPARATOR
;
163 parentDir
= storageManager
.getFileByPath(parentPath
);
166 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
);
168 while (parentDir
== null
) {
169 parentPath
= new File(parentPath
).getParent();
170 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
171 parentPath
+ OCFile
.PATH_SEPARATOR
;
172 parentDir
= storageManager
.getFileByPath(parentPath
);
174 } // exit is granted because storageManager.getFileByPath("/") never returns null
177 listDirectory(mFile
);
181 // restore index and top position
182 restoreIndexAndTopPosition();
184 } // else - should never happen now
190 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
191 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
193 if (file
.isFolder()) {
194 // update state and view of this fragment
196 // then, notify parent activity to let it update its state and view
197 mContainerActivity
.onBrowsedDownTo(file
);
198 // save index and top position
199 saveIndexAndTopPosition(position
);
201 } else { /// Click on a file
202 if (PreviewImageFragment
.canBePreviewed(file
)) {
203 // preview image - it handles the download, if needed
204 ((FileDisplayActivity
)mContainerActivity
).startImagePreview(file
);
205 } else if (PreviewTextFragment
.canBePreviewed(file
)){
206 ((FileDisplayActivity
)mContainerActivity
).startTextPreview(file
);
207 } else if (file
.isDown()) {
208 if (PreviewMediaFragment
.canBePreviewed(file
)) {
210 ((FileDisplayActivity
)mContainerActivity
).startMediaPreview(file
, 0, true
);
212 mContainerActivity
.getFileOperationsHelper().openFile(file
);
216 // automatic download, preview on finish
217 ((FileDisplayActivity
)mContainerActivity
).startDownloadForPreview(file
);
223 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
232 public void onCreateContextMenu (
233 ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
234 super.onCreateContextMenu(menu
, v
, menuInfo
);
235 Bundle args
= getArguments();
236 boolean allowContextualActions
=
237 (args
== null
) ? true
: args
.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS
, true
);
238 if (allowContextualActions
) {
239 MenuInflater inflater
= getSherlockActivity().getMenuInflater();
240 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
241 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
242 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
244 if (mContainerActivity
.getStorageManager() != null
) {
245 FileMenuFilter mf
= new FileMenuFilter(
247 mContainerActivity
.getStorageManager().getAccount(),
249 getSherlockActivity()
254 /// additional restrictions for this fragment
255 // TODO allow in the future 'open with' for previewable files
256 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
258 item
.setVisible(false
);
259 item
.setEnabled(false
);
261 /// TODO break this direct dependency on FileDisplayActivity... if possible
262 FileFragment frag
= ((FileDisplayActivity
)getSherlockActivity()).getSecondFragment();
263 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
264 frag
.getFile().getFileId() == targetFile
.getFileId()) {
265 item
= menu
.findItem(R
.id
.action_see_details
);
267 item
.setVisible(false
);
268 item
.setEnabled(false
);
279 public boolean onContextItemSelected (MenuItem item
) {
280 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
281 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
282 switch (item
.getItemId()) {
283 case R
.id
.action_share_file
: {
284 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
287 case R
.id
.action_unshare_file
: {
288 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
291 case R
.id
.action_rename_file
: {
292 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(mTargetFile
);
293 dialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_RENAME_FILE
);
296 case R
.id
.action_remove_file
: {
297 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(mTargetFile
);
298 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
301 case R
.id
.action_download_file
:
302 case R
.id
.action_sync_file
: {
303 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
306 case R
.id
.action_cancel_download
:
307 case R
.id
.action_cancel_upload
: {
308 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(mTargetFile
);
311 case R
.id
.action_see_details
: {
312 mContainerActivity
.showDetails(mTargetFile
);
315 case R
.id
.action_send_file
: {
317 if (!mTargetFile
.isDown()) { // Download the file
318 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
319 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(mTargetFile
);
322 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(mTargetFile
);
326 case R
.id
.action_move
: {
327 Intent action
= new Intent(getActivity(), MoveActivity
.class);
329 // Pass mTargetFile that contains info of selected file/folder
330 action
.putExtra(MoveActivity
.EXTRA_TARGET_FILE
, mTargetFile
);
331 getActivity().startActivityForResult(action
, FileDisplayActivity
.ACTION_MOVE_FILES
);
335 return super.onContextItemSelected(item
);
341 * Use this to query the {@link OCFile} that is currently
342 * being displayed by this fragment
343 * @return The currently viewed OCFile
345 public OCFile
getCurrentFile(){
350 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
352 public void listDirectory(){
357 * Lists the given directory on the view. When the input parameter is null,
358 * it will either refresh the last known directory. list the root
359 * if there never was a directory.
361 * @param directory File to be listed
363 public void listDirectory(OCFile directory
) {
364 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
365 if (storageManager
!= null
) {
367 // Check input parameters for null
368 if(directory
== null
){
372 directory
= storageManager
.getFileByPath("/");
373 if (directory
== null
) return; // no files, wait for sync
378 // If that's not a directory -> List its parent
379 if(!directory
.isFolder()){
380 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
381 directory
= storageManager
.getFileById(directory
.getParentId());
384 mAdapter
.swapDirectory(directory
, storageManager
);
385 if (mFile
== null
|| !mFile
.equals(directory
)) {
386 mList
.setSelectionFromTop(0, 0);
392 public void sortByName(boolean descending
) {
393 mAdapter
.setSortOrder(FileListListAdapter
.SORT_NAME
, descending
);
396 public void sortByDate(boolean descending
) {
397 mAdapter
.setSortOrder(FileListListAdapter
.SORT_DATE
, descending
);
400 public void sortBySize(boolean descending
) {
401 mAdapter
.setSortOrder(FileListListAdapter
.SORT_SIZE
, descending
);