[tx-robot] updated from transifex
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / OCFileListFragment.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author Bartek Przybylski
5 * @author masensio
6 * @author David A. Velasco
7 * Copyright (C) 2011 Bartek Przybylski
8 * Copyright (C) 2015 ownCloud Inc.
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23 package com.owncloud.android.ui.fragment;
24
25 import android.app.Activity;
26 import android.content.Intent;
27 import android.os.Bundle;
28 import android.support.v4.widget.SwipeRefreshLayout;
29 import android.view.ContextMenu;
30 import android.view.Menu;
31 import android.view.MenuInflater;
32 import android.view.MenuItem;
33 import android.view.View;
34 import android.widget.AdapterView;
35 import android.widget.AdapterView.AdapterContextMenuInfo;
36 import android.widget.PopupMenu;
37
38 import com.owncloud.android.R;
39 import com.owncloud.android.authentication.AccountUtils;
40 import com.owncloud.android.datamodel.FileDataStorageManager;
41 import com.owncloud.android.datamodel.OCFile;
42 import com.owncloud.android.files.FileMenuFilter;
43 import com.owncloud.android.lib.common.utils.Log_OC;
44 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
45 import com.owncloud.android.ui.activity.FileActivity;
46 import com.owncloud.android.ui.activity.FileDisplayActivity;
47 import com.owncloud.android.ui.activity.FolderPickerActivity;
48 import com.owncloud.android.ui.activity.OnEnforceableRefreshListener;
49 import com.owncloud.android.ui.adapter.FileListListAdapter;
50 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
51 import com.owncloud.android.ui.dialog.FileActionsDialogFragment;
52 import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
53 import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
54 import com.owncloud.android.ui.preview.PreviewImageFragment;
55 import com.owncloud.android.ui.preview.PreviewMediaFragment;
56 import com.owncloud.android.ui.preview.PreviewTextFragment;
57 import com.owncloud.android.utils.FileStorageUtils;
58
59 import java.io.File;
60
61 /**
62 * A Fragment that lists all files and folders in a given path.
63 *
64 * TODO refactor to get rid of direct dependency on FileDisplayActivity
65 */
66 public class OCFileListFragment extends ExtendedListFragment
67 implements FileActionsDialogFragment.FileActionsDialogFragmentListener {
68
69 private static final String TAG = OCFileListFragment.class.getSimpleName();
70
71 private static final String MY_PACKAGE = OCFileListFragment.class.getPackage() != null ?
72 OCFileListFragment.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
73
74 public final static String ARG_JUST_FOLDERS = MY_PACKAGE + ".JUST_FOLDERS";
75 public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS = MY_PACKAGE + ".ALLOW_CONTEXTUAL";
76
77 private static final String KEY_FILE = MY_PACKAGE + ".extra.FILE";
78
79 private FileFragment.ContainerActivity mContainerActivity;
80
81 private OCFile mFile = null;
82 private FileListListAdapter mAdapter;
83 private boolean mJustFolders;
84
85 private OCFile mTargetFile;
86
87
88
89 /**
90 * {@inheritDoc}
91 */
92 @Override
93 public void onAttach(Activity activity) {
94 super.onAttach(activity);
95 Log_OC.e(TAG, "onAttach");
96 try {
97 mContainerActivity = (FileFragment.ContainerActivity) activity;
98
99 } catch (ClassCastException e) {
100 throw new ClassCastException(activity.toString() + " must implement " +
101 FileFragment.ContainerActivity.class.getSimpleName());
102 }
103 try {
104 setOnRefreshListener((OnEnforceableRefreshListener) activity);
105
106 } catch (ClassCastException e) {
107 throw new ClassCastException(activity.toString() + " must implement " +
108 SwipeRefreshLayout.OnRefreshListener.class.getSimpleName());
109 }
110 }
111
112
113 @Override
114 public void onDetach() {
115 setOnRefreshListener(null);
116 mContainerActivity = null;
117 super.onDetach();
118 }
119
120 /**
121 * {@inheritDoc}
122 */
123 @Override
124 public void onActivityCreated(Bundle savedInstanceState) {
125 super.onActivityCreated(savedInstanceState);
126 Log_OC.e(TAG, "onActivityCreated() start");
127
128 if (savedInstanceState != null) {
129 mFile = savedInstanceState.getParcelable(KEY_FILE);
130 }
131
132 if (mJustFolders) {
133 setFooterEnabled(false);
134 } else {
135 setFooterEnabled(true);
136 }
137
138 Bundle args = getArguments();
139 mJustFolders = (args == null) ? false : args.getBoolean(ARG_JUST_FOLDERS, false);
140 mAdapter = new FileListListAdapter(
141 mJustFolders,
142 getActivity(),
143 mContainerActivity
144 );
145 setListAdapter(mAdapter);
146
147 registerLongClickListener();
148 }
149
150 private void registerLongClickListener() {
151 getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
152 public boolean onItemLongClick(AdapterView<?> arg0, View v,
153 int index, long arg3) {
154 showFileAction(index);
155 return true;
156 }
157 });
158 }
159
160
161 private void showFileAction(int fileIndex) {
162 Bundle args = getArguments();
163 PopupMenu pm = new PopupMenu(getActivity(),null);
164 Menu menu = pm.getMenu();
165
166 boolean allowContextualActions =
167 (args == null) ? true : args.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS, true);
168
169 if (allowContextualActions) {
170 MenuInflater inflater = getActivity().getMenuInflater();
171
172 inflater.inflate(R.menu.file_actions_menu, menu);
173 OCFile targetFile = (OCFile) mAdapter.getItem(fileIndex);
174
175 if (mContainerActivity.getStorageManager() != null) {
176 FileMenuFilter mf = new FileMenuFilter(
177 targetFile,
178 mContainerActivity.getStorageManager().getAccount(),
179 mContainerActivity,
180 getActivity()
181 );
182 mf.filter(menu);
183 }
184
185 /// TODO break this direct dependency on FileDisplayActivity... if possible
186 MenuItem item = menu.findItem(R.id.action_open_file_with);
187 FileFragment frag = ((FileDisplayActivity)getActivity()).getSecondFragment();
188 if (frag != null && frag instanceof FileDetailFragment &&
189 frag.getFile().getFileId() == targetFile.getFileId()) {
190 item = menu.findItem(R.id.action_see_details);
191 if (item != null) {
192 item.setVisible(false);
193 item.setEnabled(false);
194 }
195 }
196
197 FileActionsDialogFragment dialog = FileActionsDialogFragment.newInstance(menu,
198 fileIndex, targetFile.getFileName());
199 dialog.setTargetFragment(this, 0);
200 dialog.show(getFragmentManager(), FileActionsDialogFragment.FTAG_FILE_ACTIONS);
201 }
202 }
203
204 /**
205 * Saves the current listed folder.
206 */
207 @Override
208 public void onSaveInstanceState(Bundle outState) {
209 super.onSaveInstanceState(outState);
210 outState.putParcelable(KEY_FILE, mFile);
211 }
212
213 /**
214 * Call this, when the user presses the up button.
215 *
216 * Tries to move up the current folder one level. If the parent folder was removed from the
217 * database, it continues browsing up until finding an existing folders.
218 * <p/>
219 * return Count of folder levels browsed up.
220 */
221 public int onBrowseUp() {
222 OCFile parentDir = null;
223 int moveCount = 0;
224
225 if (mFile != null) {
226 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
227
228 String parentPath = null;
229 if (mFile.getParentId() != FileDataStorageManager.ROOT_PARENT_ID) {
230 parentPath = new File(mFile.getRemotePath()).getParent();
231 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
232 parentPath + OCFile.PATH_SEPARATOR;
233 parentDir = storageManager.getFileByPath(parentPath);
234 moveCount++;
235 } else {
236 parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH);
237 }
238 while (parentDir == null) {
239 parentPath = new File(parentPath).getParent();
240 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
241 parentPath + OCFile.PATH_SEPARATOR;
242 parentDir = storageManager.getFileByPath(parentPath);
243 moveCount++;
244 } // exit is granted because storageManager.getFileByPath("/") never returns null
245 mFile = parentDir;
246
247 // TODO Enable when "On Device" is recovered ?
248 listDirectory(mFile /*, MainApp.getOnlyOnDevice()*/);
249
250 onRefresh(false);
251
252 // restore index and top position
253 restoreIndexAndTopPosition();
254
255 } // else - should never happen now
256
257 return moveCount;
258 }
259
260 @Override
261 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
262 OCFile file = (OCFile) mAdapter.getItem(position);
263 if (file != null) {
264 if (file.isFolder()) {
265 // update state and view of this fragment
266 // TODO Enable when "On Device" is recovered ?
267 listDirectory(file/*, MainApp.getOnlyOnDevice()*/);
268 // then, notify parent activity to let it update its state and view
269 mContainerActivity.onBrowsedDownTo(file);
270 // save index and top position
271 saveIndexAndTopPosition(position);
272
273 } else { /// Click on a file
274 if (PreviewImageFragment.canBePreviewed(file)) {
275 // preview image - it handles the download, if needed
276 ((FileDisplayActivity)mContainerActivity).startImagePreview(file);
277 } else if (PreviewTextFragment.canBePreviewed(file)){
278 ((FileDisplayActivity)mContainerActivity).startTextPreview(file);
279 } else if (file.isDown()) {
280 if (PreviewMediaFragment.canBePreviewed(file)) {
281 // media preview
282 ((FileDisplayActivity) mContainerActivity).startMediaPreview(file, 0, true);
283 } else {
284 mContainerActivity.getFileOperationsHelper().openFile(file);
285 }
286
287 } else {
288 // automatic download, preview on finish
289 ((FileDisplayActivity) mContainerActivity).startDownloadForPreview(file);
290 }
291
292 }
293
294 } else {
295 Log_OC.d(TAG, "Null object in ListAdapter!!");
296 }
297
298 }
299
300 /**
301 * {@inheritDoc}
302 */
303 @Override
304 public void onCreateContextMenu(
305 ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
306 Bundle args = getArguments();
307 boolean allowContextualActions =
308 (args == null) ? true : args.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS, true);
309 if (allowContextualActions) {
310 MenuInflater inflater = getActivity().getMenuInflater();
311 inflater.inflate(R.menu.file_actions_menu, menu);
312 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
313 OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
314
315 if (mContainerActivity.getStorageManager() != null) {
316 FileMenuFilter mf = new FileMenuFilter(
317 targetFile,
318 mContainerActivity.getStorageManager().getAccount(),
319 mContainerActivity,
320 getActivity()
321 );
322 mf.filter(menu);
323 }
324
325 /// TODO break this direct dependency on FileDisplayActivity... if possible
326 MenuItem item = menu.findItem(R.id.action_open_file_with);
327 FileFragment frag = ((FileDisplayActivity)getActivity()).getSecondFragment();
328 if (frag != null && frag instanceof FileDetailFragment &&
329 frag.getFile().getFileId() == targetFile.getFileId()) {
330 item = menu.findItem(R.id.action_see_details);
331 if (item != null) {
332 item.setVisible(false);
333 item.setEnabled(false);
334 }
335 }
336 }
337 }
338
339 /**
340 * {@inheritDoc}
341 */
342 @Override
343 public boolean onFileActionChosen(int menuId, int filePosition) {
344 mTargetFile = (OCFile) mAdapter.getItem(filePosition);
345 switch (menuId) {
346 case R.id.action_share_file: {
347 mContainerActivity.getFileOperationsHelper().showShareFile(mTargetFile);
348 return true;
349 }
350 case R.id.action_open_file_with: {
351 mContainerActivity.getFileOperationsHelper().openFile(mTargetFile);
352 return true;
353 }
354 case R.id.action_rename_file: {
355 RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(mTargetFile);
356 dialog.show(getFragmentManager(), FileDetailFragment.FTAG_RENAME_FILE);
357 return true;
358 }
359 case R.id.action_remove_file: {
360 RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(mTargetFile);
361 dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
362 return true;
363 }
364 case R.id.action_download_file:
365 case R.id.action_sync_file: {
366 mContainerActivity.getFileOperationsHelper().syncFile(mTargetFile);
367 return true;
368 }
369 case R.id.action_cancel_sync: {
370 ((FileDisplayActivity)mContainerActivity).cancelTransference(mTargetFile);
371 return true;
372 }
373 case R.id.action_see_details: {
374 mContainerActivity.showDetails(mTargetFile);
375 return true;
376 }
377 case R.id.action_send_file: {
378 // Obtain the file
379 if (!mTargetFile.isDown()) { // Download the file
380 Log_OC.d(TAG, mTargetFile.getRemotePath() + " : File must be downloaded");
381 ((FileDisplayActivity) mContainerActivity).startDownloadForSending(mTargetFile);
382
383 } else {
384 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(mTargetFile);
385 }
386 return true;
387 }
388 case R.id.action_move: {
389 Intent action = new Intent(getActivity(), FolderPickerActivity.class);
390
391 // Pass mTargetFile that contains info of selected file/folder
392 action.putExtra(FolderPickerActivity.EXTRA_FILE, mTargetFile);
393 getActivity().startActivityForResult(action, FileDisplayActivity.ACTION_MOVE_FILES);
394 return true;
395 }
396 case R.id.action_favorite_file: {
397 mContainerActivity.getFileOperationsHelper().toggleFavorite(mTargetFile, true);
398 return true;
399 }
400 case R.id.action_unfavorite_file: {
401 mContainerActivity.getFileOperationsHelper().toggleFavorite(mTargetFile, false);
402 return true;
403 }
404 case R.id.action_copy:
405 Intent action = new Intent(getActivity(), FolderPickerActivity.class);
406
407 // Pass mTargetFile that contains info of selected file/folder
408 action.putExtra(FolderPickerActivity.EXTRA_FILE, mTargetFile);
409 getActivity().startActivityForResult(action, FileDisplayActivity.ACTION_COPY_FILES);
410 return true;
411 default:
412 return false;
413 }
414 }
415
416 /**
417 * {@inhericDoc}
418 */
419 @Override
420 public boolean onContextItemSelected (MenuItem item) {
421 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
422 boolean matched = onFileActionChosen(item.getItemId(),
423 ((AdapterContextMenuInfo) item.getMenuInfo()).position);
424 if(!matched) {
425 return super.onContextItemSelected(item);
426 } else {
427 return matched;
428 }
429 }
430
431
432 /**
433 * Use this to query the {@link OCFile} that is currently
434 * being displayed by this fragment
435 *
436 * @return The currently viewed OCFile
437 */
438 public OCFile getCurrentFile() {
439 return mFile;
440 }
441
442 /**
443 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
444 */
445 public void listDirectory(/*boolean onlyOnDevice*/){
446 listDirectory(null);
447 // TODO Enable when "On Device" is recovered ?
448 // listDirectory(null, onlyOnDevice);
449 }
450
451 public void refreshDirectory(){
452 // TODO Enable when "On Device" is recovered ?
453 listDirectory(getCurrentFile()/*, MainApp.getOnlyOnDevice()*/);
454 }
455
456 /**
457 * Lists the given directory on the view. When the input parameter is null,
458 * it will either refresh the last known directory. list the root
459 * if there never was a directory.
460 *
461 * @param directory File to be listed
462 */
463 public void listDirectory(OCFile directory/*, boolean onlyOnDevice*/) {
464 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
465 if (storageManager != null) {
466
467 // Check input parameters for null
468 if (directory == null) {
469 if (mFile != null) {
470 directory = mFile;
471 } else {
472 directory = storageManager.getFileByPath("/");
473 if (directory == null) return; // no files, wait for sync
474 }
475 }
476
477
478 // If that's not a directory -> List its parent
479 if (!directory.isFolder()) {
480 Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
481 directory = storageManager.getFileById(directory.getParentId());
482 }
483
484 // TODO Enable when "On Device" is recovered ?
485 mAdapter.swapDirectory(directory, storageManager/*, onlyOnDevice*/);
486 if (mFile == null || !mFile.equals(directory)) {
487 mCurrentListView.setSelection(0);
488 }
489 mFile = directory;
490
491 updateLayout();
492
493 }
494 }
495
496 private void updateLayout() {
497 if (!mJustFolders) {
498 int filesCount = 0, foldersCount = 0, imagesCount = 0;
499 int count = mAdapter.getCount();
500 OCFile file;
501 for (int i=0; i < count ; i++) {
502 file = (OCFile) mAdapter.getItem(i);
503 if (file.isFolder()) {
504 foldersCount++;
505 } else {
506 if (!file.isHidden()) {
507 filesCount++;
508
509 if (file.isImage()) {
510 imagesCount++;
511 }
512 }
513 }
514 }
515 // set footer text
516 setFooterText(generateFooterText(filesCount, foldersCount));
517
518 // decide grid vs list view
519 OwnCloudVersion version = AccountUtils.getServerVersion(
520 ((FileActivity)mContainerActivity).getAccount());
521 if (version != null && version.supportsRemoteThumbnails() &&
522 imagesCount > 0 && imagesCount == filesCount) {
523 switchToGridView();
524 registerLongClickListener();
525 } else {
526 switchToListView();
527 }
528 }
529 }
530
531 private String generateFooterText(int filesCount, int foldersCount) {
532 String output;
533 if (filesCount <= 0) {
534 if (foldersCount <= 0) {
535 output = "";
536
537 } else if (foldersCount == 1) {
538 output = getResources().getString(R.string.file_list__footer__folder);
539
540 } else { // foldersCount > 1
541 output = getResources().getString(R.string.file_list__footer__folders, foldersCount);
542 }
543
544 } else if (filesCount == 1) {
545 if (foldersCount <= 0) {
546 output = getResources().getString(R.string.file_list__footer__file);
547
548 } else if (foldersCount == 1) {
549 output = getResources().getString(R.string.file_list__footer__file_and_folder);
550
551 } else { // foldersCount > 1
552 output = getResources().getString(R.string.file_list__footer__file_and_folders, foldersCount);
553 }
554 } else { // filesCount > 1
555 if (foldersCount <= 0) {
556 output = getResources().getString(R.string.file_list__footer__files, filesCount);
557
558 } else if (foldersCount == 1) {
559 output = getResources().getString(R.string.file_list__footer__files_and_folder, filesCount);
560
561 } else { // foldersCount > 1
562 output = getResources().getString(
563 R.string.file_list__footer__files_and_folders, filesCount, foldersCount
564 );
565
566 }
567 }
568 return output;
569 }
570
571 public void sortByName(boolean descending) {
572 mAdapter.setSortOrder(FileStorageUtils.SORT_NAME, descending);
573 }
574
575 public void sortByDate(boolean descending) {
576 mAdapter.setSortOrder(FileStorageUtils.SORT_DATE, descending);
577 }
578
579 public void sortBySize(boolean descending) {
580 mAdapter.setSortOrder(FileStorageUtils.SORT_SIZE, descending);
581 }
582 }