Removed call to methods to check transfer state of files through binders and replaced...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / OCFileListFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2014 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18 package com.owncloud.android.ui.fragment;
19
20 import java.io.File;
21 import java.util.Vector;
22
23 import android.app.Activity;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.os.Bundle;
27 import android.support.v4.widget.SwipeRefreshLayout;
28 import android.view.ContextMenu;
29 import android.view.MenuInflater;
30 import android.view.MenuItem;
31 import android.view.View;
32 import android.widget.AdapterView;
33 import android.widget.AdapterView.AdapterContextMenuInfo;
34 import android.widget.TextView;
35 import android.view.LayoutInflater;
36
37 import com.owncloud.android.R;
38 import com.owncloud.android.datamodel.FileDataStorageManager;
39 import com.owncloud.android.datamodel.OCFile;
40 import com.owncloud.android.files.FileMenuFilter;
41 import com.owncloud.android.lib.common.utils.Log_OC;
42 import com.owncloud.android.ui.activity.FileDisplayActivity;
43 import com.owncloud.android.ui.activity.FolderPickerActivity;
44 import com.owncloud.android.ui.activity.OnEnforceableRefreshListener;
45 import com.owncloud.android.ui.adapter.FileListListAdapter;
46 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
47 import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
48 import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
49 import com.owncloud.android.ui.preview.PreviewImageFragment;
50 import com.owncloud.android.ui.preview.PreviewMediaFragment;
51 import com.owncloud.android.utils.FileStorageUtils;
52
53 /**
54 * A Fragment that lists all files and folders in a given path.
55 *
56 * TODO refactorize to get rid of direct dependency on FileDisplayActivity
57 *
58 * @author Bartek Przybylski
59 * @author masensio
60 * @author David A. Velasco
61 */
62 public class OCFileListFragment extends ExtendedListFragment {
63
64 private static final String TAG = OCFileListFragment.class.getSimpleName();
65
66 private static final String MY_PACKAGE = OCFileListFragment.class.getPackage() != null ?
67 OCFileListFragment.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
68
69 public final static String ARG_JUST_FOLDERS = MY_PACKAGE + ".JUST_FOLDERS";
70 public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS = MY_PACKAGE + ".ALLOW_CONTEXTUAL";
71
72 private static final String KEY_FILE = MY_PACKAGE + ".extra.FILE";
73
74 private FileFragment.ContainerActivity mContainerActivity;
75
76 private OCFile mFile = null;
77 private FileListListAdapter mAdapter;
78 private View mFooterView;
79
80 private OCFile mTargetFile;
81
82
83 /**
84 * {@inheritDoc}
85 */
86 @Override
87 public void onAttach(Activity activity) {
88 super.onAttach(activity);
89 Log_OC.e(TAG, "onAttach");
90 try {
91 mContainerActivity = (FileFragment.ContainerActivity) activity;
92
93 } catch (ClassCastException e) {
94 throw new ClassCastException(activity.toString() + " must implement " +
95 FileFragment.ContainerActivity.class.getSimpleName());
96 }
97 try {
98 setOnRefreshListener((OnEnforceableRefreshListener) activity);
99
100 } catch (ClassCastException e) {
101 throw new ClassCastException(activity.toString() + " must implement " +
102 SwipeRefreshLayout.OnRefreshListener.class.getSimpleName());
103 }
104 }
105
106
107 @Override
108 public void onDetach() {
109 setOnRefreshListener(null);
110 mContainerActivity = null;
111 super.onDetach();
112 }
113
114 /**
115 * {@inheritDoc}
116 */
117 @Override
118 public void onActivityCreated(Bundle savedInstanceState) {
119 super.onActivityCreated(savedInstanceState);
120 Log_OC.e(TAG, "onActivityCreated() start");
121
122 if (savedInstanceState != null) {
123 mFile = savedInstanceState.getParcelable(KEY_FILE);
124 }
125
126 mFooterView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
127 R.layout.list_footer, null, false);
128 setFooterView(mFooterView);
129
130 Bundle args = getArguments();
131 boolean justFolders = (args == null) ? false : args.getBoolean(ARG_JUST_FOLDERS, false);
132 mAdapter = new FileListListAdapter(
133 justFolders,
134 getSherlockActivity()
135 );
136 setListAdapter(mAdapter);
137
138 registerForContextMenu(getListView());
139 getListView().setOnCreateContextMenuListener(this);
140 }
141
142 /**
143 * Saves the current listed folder.
144 */
145 @Override
146 public void onSaveInstanceState (Bundle outState) {
147 super.onSaveInstanceState(outState);
148 outState.putParcelable(KEY_FILE, mFile);
149 }
150
151 /**
152 * Call this, when the user presses the up button.
153 *
154 * Tries to move up the current folder one level. If the parent folder was removed from the
155 * database, it continues browsing up until finding an existing folders.
156 *
157 * return Count of folder levels browsed up.
158 */
159 public int onBrowseUp() {
160 OCFile parentDir = null;
161 int moveCount = 0;
162
163 if(mFile != null){
164 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
165
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 :
170 parentPath + OCFile.PATH_SEPARATOR;
171 parentDir = storageManager.getFileByPath(parentPath);
172 moveCount++;
173 } else {
174 parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH);
175 }
176 while (parentDir == null) {
177 parentPath = new File(parentPath).getParent();
178 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
179 parentPath + OCFile.PATH_SEPARATOR;
180 parentDir = storageManager.getFileByPath(parentPath);
181 moveCount++;
182 } // exit is granted because storageManager.getFileByPath("/") never returns null
183 mFile = parentDir;
184
185 listDirectory(mFile);
186
187 onRefresh(false);
188
189 // restore index and top position
190 restoreIndexAndTopPosition();
191
192 } // else - should never happen now
193
194 return moveCount;
195 }
196
197 @Override
198 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
199 OCFile file = (OCFile) mAdapter.getItem(position);
200 if (file != null) {
201 if (file.isFolder()) {
202 // update state and view of this fragment
203 listDirectory(file);
204 // then, notify parent activity to let it update its state and view
205 mContainerActivity.onBrowsedDownTo(file);
206 // save index and top position
207 saveIndexAndTopPosition(position);
208
209 } else { /// Click on a file
210 if (PreviewImageFragment.canBePreviewed(file)) {
211 // preview image - it handles the download, if needed
212 ((FileDisplayActivity)mContainerActivity).startImagePreview(file);
213
214 } else if (file.isDown()) {
215 if (PreviewMediaFragment.canBePreviewed(file)) {
216 // media preview
217 ((FileDisplayActivity)mContainerActivity).startMediaPreview(file, 0, true);
218 } else {
219 mContainerActivity.getFileOperationsHelper().openFile(file);
220 }
221
222 } else {
223 // automatic download, preview on finish
224 ((FileDisplayActivity)mContainerActivity).startDownloadForPreview(file);
225 }
226
227 }
228
229 } else {
230 Log_OC.d(TAG, "Null object in ListAdapter!!");
231 }
232
233 }
234
235 /**
236 * {@inheritDoc}
237 */
238 @Override
239 public void onCreateContextMenu (
240 ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
241 super.onCreateContextMenu(menu, v, menuInfo);
242 Bundle args = getArguments();
243 boolean allowContextualActions =
244 (args == null) ? true : args.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS, true);
245 if (allowContextualActions) {
246 MenuInflater inflater = getSherlockActivity().getMenuInflater();
247 inflater.inflate(R.menu.file_actions_menu, menu);
248 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
249 OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
250
251 if (mContainerActivity.getStorageManager() != null) {
252 FileMenuFilter mf = new FileMenuFilter(
253 targetFile,
254 mContainerActivity.getStorageManager().getAccount(),
255 getSherlockActivity()
256 );
257 mf.filter(menu);
258 }
259
260 /// TODO break this direct dependency on FileDisplayActivity... if possible
261 MenuItem item = menu.findItem(R.id.action_open_file_with);
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);
266 if (item != null) {
267 item.setVisible(false);
268 item.setEnabled(false);
269 }
270 }
271 }
272 }
273
274
275 /**
276 * {@inhericDoc}
277 */
278 @Override
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);
285 return true;
286 }
287 case R.id.action_open_file_with: {
288 mContainerActivity.getFileOperationsHelper().openFile(mTargetFile);
289 return true;
290 }
291 case R.id.action_unshare_file: {
292 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(mTargetFile);
293 return true;
294 }
295 case R.id.action_rename_file: {
296 RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(mTargetFile);
297 dialog.show(getFragmentManager(), FileDetailFragment.FTAG_RENAME_FILE);
298 return true;
299 }
300 case R.id.action_remove_file: {
301 RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(mTargetFile);
302 dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
303 return true;
304 }
305 case R.id.action_download_file:
306 case R.id.action_sync_file: {
307 mContainerActivity.getFileOperationsHelper().syncFile(mTargetFile);
308 return true;
309 }
310 case R.id.action_cancel_download:
311 case R.id.action_cancel_upload: {
312 ((FileDisplayActivity)mContainerActivity).cancelTransference(mTargetFile);
313 return true;
314 }
315 case R.id.action_see_details: {
316 mContainerActivity.showDetails(mTargetFile);
317 return true;
318 }
319 case R.id.action_send_file: {
320 // Obtain the file
321 if (!mTargetFile.isDown()) { // Download the file
322 Log_OC.d(TAG, mTargetFile.getRemotePath() + " : File must be downloaded");
323 ((FileDisplayActivity)mContainerActivity).startDownloadForSending(mTargetFile);
324
325 } else {
326 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(mTargetFile);
327 }
328 return true;
329 }
330 case R.id.action_move: {
331 Intent action = new Intent(getActivity(), FolderPickerActivity.class);
332
333 // Pass mTargetFile that contains info of selected file/folder
334 action.putExtra(FolderPickerActivity.EXTRA_FILE, mTargetFile);
335 getActivity().startActivityForResult(action, FileDisplayActivity.ACTION_MOVE_FILES);
336 return true;
337 }
338 default:
339 return super.onContextItemSelected(item);
340 }
341 }
342
343
344 /**
345 * Use this to query the {@link OCFile} that is currently
346 * being displayed by this fragment
347 * @return The currently viewed OCFile
348 */
349 public OCFile getCurrentFile(){
350 return mFile;
351 }
352
353 /**
354 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
355 */
356 public void listDirectory(){
357 listDirectory(null);
358 }
359
360 /**
361 * Lists the given directory on the view. When the input parameter is null,
362 * it will either refresh the last known directory. list the root
363 * if there never was a directory.
364 *
365 * @param directory File to be listed
366 */
367 public void listDirectory(OCFile directory) {
368 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
369 if (storageManager != null) {
370
371 // Check input parameters for null
372 if(directory == null){
373 if(mFile != null){
374 directory = mFile;
375 } else {
376 directory = storageManager.getFileByPath("/");
377 if (directory == null) return; // no files, wait for sync
378 }
379 }
380
381
382 // If that's not a directory -> List its parent
383 if(!directory.isFolder()){
384 Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
385 directory = storageManager.getFileById(directory.getParentId());
386 }
387
388 mAdapter.swapDirectory(directory, storageManager);
389 if (mFile == null || !mFile.equals(directory)) {
390 mList.setSelectionFromTop(0, 0);
391 }
392 mFile = directory;
393
394 // Update Footer
395 TextView footerText = (TextView) mFooterView.findViewById(R.id.footerText);
396 Log_OC.d("footer", String.valueOf(System.currentTimeMillis()));
397 footerText.setText(generateFooterText(directory));
398 Log_OC.d("footer", String.valueOf(System.currentTimeMillis()));
399 }
400 }
401
402 private String generateFooterText(OCFile directory) {
403 Integer files = 0;
404 Integer folders = 0;
405
406 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
407 Vector<OCFile> mFiles = storageManager.getFolderContent(mFile);
408
409 for (OCFile ocFile : mFiles) {
410 if (ocFile.isFolder()) {
411 folders++;
412 } else {
413 files++;
414 }
415 }
416
417 String output = "";
418
419 if (files > 0){
420 if (files == 1) {
421 output = output + files.toString() + " " + getResources().getString(R.string.file_list_file);
422 } else {
423 output = output + files.toString() + " " + getResources().getString(R.string.file_list_files);
424 }
425 }
426 if (folders > 0 && files > 0){
427 output = output + ", ";
428 }
429 if (folders == 1) {
430 output = output + folders.toString() + " " + getResources().getString(R.string.file_list_folder);
431 } else if (folders > 1) {
432 output = output + folders.toString() + " " + getResources().getString(R.string.file_list_folders);
433 }
434
435 return output;
436 }
437
438 public void sortByName(boolean descending) {
439 mAdapter.setSortOrder(FileStorageUtils.SORT_NAME, descending);
440 }
441
442 public void sortByDate(boolean descending) {
443 mAdapter.setSortOrder(FileStorageUtils.SORT_DATE, descending);
444 }
445
446 public void sortBySize(boolean descending) {
447 mAdapter.setSortOrder(FileStorageUtils.SORT_SIZE, descending);
448 }
449
450 }