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
.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
.ui
.activity
.FileDisplayActivity
;
37 import com
.owncloud
.android
.ui
.activity
.MoveActivity
;
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
;
44 import com
.owncloud
.android
.utils
.Log_OC
;
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
{
57 private static final String TAG
= OCFileListFragment
.class.getSimpleName();
59 private static final String MY_PACKAGE
= OCFileListFragment
.class.getPackage() != null ?
60 OCFileListFragment
.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
61 private static final String EXTRA_FILE
= MY_PACKAGE
+ ".extra.FILE";
63 private FileFragment
.ContainerActivity mContainerActivity
;
65 private OCFile mFile
= null
;
66 private FileListListAdapter mAdapter
;
68 private OCFile mTargetFile
;
74 public void onAttach(Activity activity
) {
75 super.onAttach(activity
);
76 Log_OC
.e(TAG
, "onAttach");
78 mContainerActivity
= (FileFragment
.ContainerActivity
) activity
;
79 } catch (ClassCastException e
) {
80 throw new ClassCastException(activity
.toString() + " must implement " +
81 FileFragment
.ContainerActivity
.class.getSimpleName());
87 public void onDetach() {
88 mContainerActivity
= null
;
96 public void onActivityCreated(Bundle savedInstanceState
) {
97 super.onActivityCreated(savedInstanceState
);
98 Log_OC
.e(TAG
, "onActivityCreated() start");
100 if (savedInstanceState
!= null
) {
101 mFile
= savedInstanceState
.getParcelable(EXTRA_FILE
);
104 mAdapter
= new FileListListAdapter(getSherlockActivity(), mContainerActivity
);
105 setListAdapter(mAdapter
);
107 registerForContextMenu(getListView());
108 getListView().setOnCreateContextMenuListener(this);
112 * Saves the current listed folder.
115 public void onSaveInstanceState (Bundle outState
) {
116 super.onSaveInstanceState(outState
);
117 outState
.putParcelable(EXTRA_FILE
, mFile
);
121 * Call this, when the user presses the up button.
123 * Tries to move up the current folder one level. If the parent folder was removed from the
124 * database, it continues browsing up until finding an existing folders.
126 * return Count of folder levels browsed up.
128 public int onBrowseUp() {
129 OCFile parentDir
= null
;
133 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
135 String parentPath
= null
;
136 if (mFile
.getParentId() != FileDataStorageManager
.ROOT_PARENT_ID
) {
137 parentPath
= new File(mFile
.getRemotePath()).getParent();
138 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
139 parentPath
+ OCFile
.PATH_SEPARATOR
;
140 parentDir
= storageManager
.getFileByPath(parentPath
);
143 parentDir
= storageManager
.getFileByPath(OCFile
.ROOT_PATH
);
145 while (parentDir
== null
) {
146 parentPath
= new File(parentPath
).getParent();
147 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
:
148 parentPath
+ OCFile
.PATH_SEPARATOR
;
149 parentDir
= storageManager
.getFileByPath(parentPath
);
151 } // exit is granted because storageManager.getFileByPath("/") never returns null
154 listDirectory(mFile
);
156 ((FileDisplayActivity
)mContainerActivity
).startSyncFolderOperation(mFile
);
158 // restore index and top position
159 restoreIndexAndTopPosition();
161 } // else - should never happen now
167 public void onItemClick(AdapterView
<?
> l
, View v
, int position
, long id
) {
168 OCFile file
= (OCFile
) mAdapter
.getItem(position
);
170 if (file
.isFolder()) {
171 // update state and view of this fragment
173 // then, notify parent activity to let it update its state and view
174 mContainerActivity
.onBrowsedDownTo(file
);
175 // save index and top position
176 saveIndexAndTopPosition(position
);
178 } else { /// Click on a file
179 if (PreviewImageFragment
.canBePreviewed(file
)) {
180 // preview image - it handles the download, if needed
181 ((FileDisplayActivity
)mContainerActivity
).startImagePreview(file
);
183 } else if (file
.isDown()) {
184 if (PreviewMediaFragment
.canBePreviewed(file
)) {
186 ((FileDisplayActivity
)mContainerActivity
).startMediaPreview(file
, 0, true
);
188 mContainerActivity
.getFileOperationsHelper().openFile(file
);
192 // automatic download, preview on finish
193 ((FileDisplayActivity
)mContainerActivity
).startDownloadForPreview(file
);
199 Log_OC
.d(TAG
, "Null object in ListAdapter!!");
208 public void onCreateContextMenu (
209 ContextMenu menu
, View v
, ContextMenu
.ContextMenuInfo menuInfo
) {
210 super.onCreateContextMenu(menu
, v
, menuInfo
);
211 MenuInflater inflater
= getSherlockActivity().getMenuInflater();
212 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
213 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) menuInfo
;
214 OCFile targetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
216 if (mContainerActivity
.getStorageManager() != null
) {
217 FileMenuFilter mf
= new FileMenuFilter(
219 mContainerActivity
.getStorageManager().getAccount(),
221 getSherlockActivity()
226 /// additional restrictions for this fragment
227 // TODO allow in the future 'open with' for previewable files
228 MenuItem item
= menu
.findItem(R
.id
.action_open_file_with
);
230 item
.setVisible(false
);
231 item
.setEnabled(false
);
233 /// TODO break this direct dependency on FileDisplayActivity... if possible
234 FileFragment frag
= ((FileDisplayActivity
)getSherlockActivity()).getSecondFragment();
235 if (frag
!= null
&& frag
instanceof FileDetailFragment
&&
236 frag
.getFile().getFileId() == targetFile
.getFileId()) {
237 item
= menu
.findItem(R
.id
.action_see_details
);
239 item
.setVisible(false
);
240 item
.setEnabled(false
);
252 public boolean onContextItemSelected (MenuItem item
) {
253 AdapterContextMenuInfo info
= (AdapterContextMenuInfo
) item
.getMenuInfo();
254 mTargetFile
= (OCFile
) mAdapter
.getItem(info
.position
);
255 switch (item
.getItemId()) {
256 case R
.id
.action_share_file
: {
257 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(mTargetFile
);
260 case R
.id
.action_unshare_file
: {
261 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(mTargetFile
);
264 case R
.id
.action_rename_file
: {
265 RenameFileDialogFragment dialog
= RenameFileDialogFragment
.newInstance(mTargetFile
);
266 dialog
.show(getFragmentManager(), FileDetailFragment
.FTAG_RENAME_FILE
);
269 case R
.id
.action_remove_file
: {
270 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(mTargetFile
);
271 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
274 case R
.id
.action_download_file
:
275 case R
.id
.action_sync_file
: {
276 mContainerActivity
.getFileOperationsHelper().syncFile(mTargetFile
);
279 case R
.id
.action_cancel_download
:
280 case R
.id
.action_cancel_upload
: {
281 ((FileDisplayActivity
)mContainerActivity
).cancelTransference(mTargetFile
);
284 case R
.id
.action_see_details
: {
285 mContainerActivity
.showDetails(mTargetFile
);
288 case R
.id
.action_send_file
: {
290 if (!mTargetFile
.isDown()) { // Download the file
291 Log_OC
.d(TAG
, mTargetFile
.getRemotePath() + " : File must be downloaded");
292 ((FileDisplayActivity
)mContainerActivity
).startDownloadForSending(mTargetFile
);
295 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(mTargetFile
);
299 case R
.id
.action_move
: {
300 Intent i
= new Intent(getActivity(), MoveActivity
.class);
306 return super.onContextItemSelected(item
);
312 * Use this to query the {@link OCFile} that is currently
313 * being displayed by this fragment
314 * @return The currently viewed OCFile
316 public OCFile
getCurrentFile(){
321 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
323 public void listDirectory(){
328 * Lists the given directory on the view. When the input parameter is null,
329 * it will either refresh the last known directory. list the root
330 * if there never was a directory.
332 * @param directory File to be listed
334 public void listDirectory(OCFile directory
) {
335 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
336 if (storageManager
!= null
) {
338 // Check input parameters for null
339 if(directory
== null
){
343 directory
= storageManager
.getFileByPath("/");
344 if (directory
== null
) return; // no files, wait for sync
349 // If that's not a directory -> List its parent
350 if(!directory
.isFolder()){
351 Log_OC
.w(TAG
, "You see, that is not a directory -> " + directory
.toString());
352 directory
= storageManager
.getFileById(directory
.getParentId());
355 mAdapter
.swapDirectory(directory
, storageManager
);
356 if (mFile
== null
|| !mFile
.equals(directory
)) {
357 mList
.setSelectionFromTop(0, 0);
365 public void onRefresh() {
370 mFile
= mContainerActivity
.getStorageManager().getFileById(mFile
.getFileId());
372 listDirectory(mFile
);
374 ((FileDisplayActivity
)mContainerActivity
).startSyncFolderOperation(mFile
);