d6e2e163d000781e7c15ea435b4c5e76b4711247
[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.ArrayList;
22 import java.util.List;
23
24 import com.owncloud.android.R;
25 import com.owncloud.android.authentication.AccountUtils;
26 import com.owncloud.android.datamodel.FileDataStorageManager;
27 import com.owncloud.android.datamodel.FileListCursorLoader;
28 import com.owncloud.android.datamodel.OCFile;
29 import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
30 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
31 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
32 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
33 import com.owncloud.android.lib.common.operations.RemoteOperation;
34 import com.owncloud.android.operations.RemoveFileOperation;
35 import com.owncloud.android.operations.RenameFileOperation;
36 import com.owncloud.android.operations.SynchronizeFileOperation;
37 import com.owncloud.android.ui.activity.FileActivity;
38 import com.owncloud.android.ui.ExtendedListView;
39 import com.owncloud.android.ui.activity.TransferServiceGetter;
40 import com.owncloud.android.ui.adapter.FileListListAdapter;
41 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
42 import com.owncloud.android.ui.dialog.EditNameDialog;
43 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
44 import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;
45 import com.owncloud.android.ui.preview.PreviewImageFragment;
46 import com.owncloud.android.ui.preview.PreviewMediaFragment;
47 import com.owncloud.android.utils.Log_OC;
48
49 import android.accounts.Account;
50 import android.app.Activity;
51 import android.database.Cursor;
52 import android.net.Uri;
53 import android.os.Bundle;
54 import android.os.Handler;
55 import android.support.v4.app.LoaderManager;
56 import android.support.v4.app.LoaderManager.LoaderCallbacks;
57 import android.support.v4.content.Loader;
58 import android.view.ContextMenu;
59 import android.view.MenuInflater;
60 import android.view.MenuItem;
61 import android.view.View;
62 import android.widget.AdapterView;
63 import android.widget.AdapterView.AdapterContextMenuInfo;
64
65 /**
66 * A Fragment that lists all files and folders in a given path.
67 *
68 * @author Bartek Przybylski
69 *
70 */
71 public class OCFileListFragment extends ExtendedListFragment
72 implements EditNameDialogListener, ConfirmationDialogFragmentListener,
73 LoaderCallbacks<Cursor>{
74
75 private static final String TAG = OCFileListFragment.class.getSimpleName();
76
77 private static final String MY_PACKAGE = OCFileListFragment.class.getPackage() != null ? OCFileListFragment.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
78 private static final String EXTRA_FILE = MY_PACKAGE + ".extra.FILE";
79
80 private static final String KEY_INDEXES = "INDEXES";
81 private static final String KEY_FIRST_POSITIONS= "FIRST_POSITIONS";
82 private static final String KEY_TOPS = "TOPS";
83 private static final String KEY_HEIGHT_CELL = "HEIGHT_CELL";
84
85 private static final int LOADER_ID = 0;
86
87 private OCFileListFragment.ContainerActivity mContainerActivity;
88
89 private OCFile mFile = null;
90 private FileListListAdapter mAdapter;
91 private LoaderManager mLoaderManager;
92 private FileListCursorLoader mCursorLoader;
93
94 private Handler mHandler;
95 private OCFile mTargetFile;
96
97 // Save the state of the scroll in browsing
98 private ArrayList<Integer> mIndexes;
99 private ArrayList<Integer> mFirstPositions;
100 private ArrayList<Integer> mTops;
101
102 private int mHeightCell = 0;
103
104 /**
105 * {@inheritDoc}
106 */
107 @Override
108 public void onAttach(Activity activity) {
109 super.onAttach(activity);
110 Log_OC.e(TAG, "onAttach");
111 try {
112 mContainerActivity = (ContainerActivity) activity;
113 } catch (ClassCastException e) {
114 throw new ClassCastException(activity.toString() + " must implement " + OCFileListFragment.ContainerActivity.class.getSimpleName());
115 }
116 }
117
118
119 /**
120 * {@inheritDoc}
121 */
122 @Override
123 public void onActivityCreated(Bundle savedInstanceState) {
124 super.onActivityCreated(savedInstanceState);
125 Log_OC.e(TAG, "onActivityCreated() start");
126
127 mAdapter = new FileListListAdapter(getSherlockActivity(), mContainerActivity);
128 mLoaderManager = getLoaderManager();
129
130 if (savedInstanceState != null) {
131 mFile = savedInstanceState.getParcelable(EXTRA_FILE);
132 mIndexes = savedInstanceState.getIntegerArrayList(KEY_INDEXES);
133 mFirstPositions = savedInstanceState.getIntegerArrayList(KEY_FIRST_POSITIONS);
134 mTops = savedInstanceState.getIntegerArrayList(KEY_TOPS);
135 mHeightCell = savedInstanceState.getInt(KEY_HEIGHT_CELL);
136
137 } else {
138 mIndexes = new ArrayList<Integer>();
139 mFirstPositions = new ArrayList<Integer>();
140 mTops = new ArrayList<Integer>();
141 mHeightCell = 0;
142
143 }
144
145 // Initialize loaderManager and makes it active
146 mLoaderManager.initLoader(LOADER_ID, null, this);
147
148 setListAdapter(mAdapter);
149
150 registerForContextMenu(getListView());
151 getListView().setOnCreateContextMenuListener(this);
152
153 mHandler = new Handler();
154
155
156 }
157
158 /**
159 * Saves the current listed folder.
160 */
161 @Override
162 public void onSaveInstanceState (Bundle outState) {
163 super.onSaveInstanceState(outState);
164 outState.putParcelable(EXTRA_FILE, mFile);
165 outState.putIntegerArrayList(KEY_INDEXES, mIndexes);
166 outState.putIntegerArrayList(KEY_FIRST_POSITIONS, mFirstPositions);
167 outState.putIntegerArrayList(KEY_TOPS, mTops);
168 outState.putInt(KEY_HEIGHT_CELL, mHeightCell);
169 }
170
171 /**
172 * Call this, when the user presses the up button.
173 *
174 * Tries to move up the current folder one level. If the parent folder was removed from the database,
175 * it continues browsing up until finding an existing folders.
176 *
177 * return Count of folder levels browsed up.
178 */
179 public int onBrowseUp() {
180 OCFile parentDir = null;
181 int moveCount = 0;
182
183 if(mFile != null){
184 FileDataStorageManager storageManager =
185 ((FileActivity)getSherlockActivity()).getStorageManager();
186
187 String parentPath = null;
188 if (mFile.getParentId() != FileDataStorageManager.ROOT_PARENT_ID) {
189 parentPath = new File(mFile.getRemotePath()).getParent();
190 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
191 parentDir = storageManager.getFileByPath(parentPath);
192 moveCount++;
193 } else {
194 parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH); // never returns null; keep the path in root folder
195 }
196 while (parentDir == null) {
197 parentPath = new File(parentPath).getParent();
198 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
199 parentDir = storageManager.getFileByPath(parentPath);
200 moveCount++;
201 } // exit is granted because storageManager.getFileByPath("/") never returns null
202 mFile = parentDir;
203 }
204
205 if (mFile != null) {
206 listDirectory(mFile);
207
208 mContainerActivity.startSyncFolderOperation(mFile);
209
210 // restore index and top position
211 restoreIndexAndTopPosition();
212
213 } // else - should never happen now
214
215 return moveCount;
216 }
217
218 /*
219 * Restore index and position
220 */
221 private void restoreIndexAndTopPosition() {
222 if (mIndexes.size() > 0) {
223 // needs to be checked; not every browse-up had a browse-down before
224
225 int index = mIndexes.remove(mIndexes.size() - 1);
226
227 int firstPosition = mFirstPositions.remove(mFirstPositions.size() -1);
228
229 int top = mTops.remove(mTops.size() - 1);
230
231 ExtendedListView list = (ExtendedListView) getListView();
232 list.setSelectionFromTop(firstPosition, top);
233
234 // Move the scroll if the selection is not visible
235 int indexPosition = mHeightCell*index;
236 int height = list.getHeight();
237
238 if (indexPosition > height) {
239 if (android.os.Build.VERSION.SDK_INT >= 11)
240 {
241 list.smoothScrollToPosition(index);
242 }
243 else if (android.os.Build.VERSION.SDK_INT >= 8)
244 {
245 list.setSelectionFromTop(index, 0);
246 }
247
248 }
249 }
250 }
251
252 /*
253 * Save index and top position
254 */
255 private void saveIndexAndTopPosition(int index) {
256
257 mIndexes.add(index);
258
259 ExtendedListView list = (ExtendedListView) getListView();
260
261 int firstPosition = list.getFirstVisiblePosition();
262 mFirstPositions.add(firstPosition);
263
264 View view = list.getChildAt(0);
265 int top = (view == null) ? 0 : view.getTop() ;
266
267 mTops.add(top);
268
269 // Save the height of a cell
270 mHeightCell = (view == null || mHeightCell != 0) ? mHeightCell : view.getHeight();
271 }
272
273 @Override
274 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
275 OCFile file = ((FileActivity)getSherlockActivity()).getStorageManager().createFileInstance(
276 (Cursor) mAdapter.getItem(position));
277 if (file != null) {
278 if (file.isFolder()) {
279 // update state and view of this fragment
280 listDirectory(file);
281 // then, notify parent activity to let it update its state and view, and other fragments
282 mContainerActivity.onBrowsedDownTo(file);
283 // save index and top position
284 saveIndexAndTopPosition(position);
285
286 } else { /// Click on a file
287 if (PreviewImageFragment.canBePreviewed(file)) {
288 // preview image - it handles the download, if needed
289 mContainerActivity.startImagePreview(file);
290
291 } else if (file.isDown()) {
292 if (PreviewMediaFragment.canBePreviewed(file)) {
293 // media preview
294 mContainerActivity.startMediaPreview(file, 0, true);
295 } else {
296 FileActivity activity = (FileActivity) getSherlockActivity();
297 activity.getFileOperationsHelper().openFile(file, activity);
298 }
299
300 } else {
301 // automatic download, preview on finish
302 mContainerActivity.startDownloadForPreview(file);
303 }
304
305 }
306
307 } else {
308 Log_OC.d(TAG, "Null object in ListAdapter!!");
309 }
310
311 }
312
313 /**
314 * {@inheritDoc}
315 */
316 @Override
317 public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
318 super.onCreateContextMenu(menu, v, menuInfo);
319 MenuInflater inflater = getSherlockActivity().getMenuInflater();
320 inflater.inflate(R.menu.file_actions_menu, menu);
321 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
322 OCFile targetFile = ((FileActivity)getSherlockActivity()).getStorageManager().createFileInstance(
323 (Cursor) mAdapter.getItem(info.position));
324 List<Integer> toHide = new ArrayList<Integer>();
325 List<Integer> toDisable = new ArrayList<Integer>();
326
327 MenuItem item = null;
328 if (targetFile.isFolder()) {
329 // contextual menu for folders
330 toHide.add(R.id.action_open_file_with);
331 toHide.add(R.id.action_download_file);
332 toHide.add(R.id.action_cancel_download);
333 toHide.add(R.id.action_cancel_upload);
334 toHide.add(R.id.action_sync_file);
335 toHide.add(R.id.action_see_details);
336 toHide.add(R.id.action_send_file);
337 if ( mContainerActivity.getFileDownloaderBinder().isDownloading(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile) ||
338 mContainerActivity.getFileUploaderBinder().isUploading(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile) ) {
339 toDisable.add(R.id.action_rename_file);
340 toDisable.add(R.id.action_remove_file);
341
342 }
343
344 } else {
345 // contextual menu for regular files
346
347 // new design: 'download' and 'open with' won't be available anymore in context menu
348 toHide.add(R.id.action_download_file);
349 toHide.add(R.id.action_open_file_with);
350
351 if (targetFile.isDown()) {
352 toHide.add(R.id.action_cancel_download);
353 toHide.add(R.id.action_cancel_upload);
354
355 } else {
356 toHide.add(R.id.action_sync_file);
357 }
358 if ( mContainerActivity.getFileDownloaderBinder().isDownloading(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile)) {
359 toHide.add(R.id.action_cancel_upload);
360 toDisable.add(R.id.action_rename_file);
361 toDisable.add(R.id.action_remove_file);
362
363 } else if ( mContainerActivity.getFileUploaderBinder().isUploading(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile)) {
364 toHide.add(R.id.action_cancel_download);
365 toDisable.add(R.id.action_rename_file);
366 toDisable.add(R.id.action_remove_file);
367
368 } else {
369 toHide.add(R.id.action_cancel_download);
370 toHide.add(R.id.action_cancel_upload);
371 }
372 }
373
374 // Options shareLink
375 if (!targetFile.isShareByLink()) {
376 toHide.add(R.id.action_unshare_file);
377 }
378
379 // Send file
380 boolean sendEnabled = getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on");
381 if (!sendEnabled) {
382 toHide.add(R.id.action_send_file);
383 }
384
385 for (int i : toHide) {
386 item = menu.findItem(i);
387 if (item != null) {
388 item.setVisible(false);
389 item.setEnabled(false);
390 }
391 }
392
393 for (int i : toDisable) {
394 item = menu.findItem(i);
395 if (item != null) {
396 item.setEnabled(false);
397 }
398 }
399 }
400
401
402 /**
403 * {@inhericDoc}
404 */
405 @Override
406 public boolean onContextItemSelected (MenuItem item) {
407 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
408 mTargetFile = ((FileActivity)getSherlockActivity()).getStorageManager().createFileInstance(
409 (Cursor) mAdapter.getItem(info.position));
410 switch (item.getItemId()) {
411 case R.id.action_share_file: {
412 FileActivity activity = (FileActivity) getSherlockActivity();
413 activity.getFileOperationsHelper().shareFileWithLink(mTargetFile, activity);
414 return true;
415 }
416 case R.id.action_unshare_file: {
417 FileActivity activity = (FileActivity) getSherlockActivity();
418 activity.getFileOperationsHelper().unshareFileWithLink(mTargetFile, activity);
419 return true;
420 }
421 case R.id.action_rename_file: {
422 String fileName = mTargetFile.getFileName();
423 int extensionStart = mTargetFile.isFolder() ? -1 : fileName.lastIndexOf(".");
424 int selectionEnd = (extensionStart >= 0) ? extensionStart : fileName.length();
425 EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.rename_dialog_title), fileName, 0, selectionEnd, this);
426 dialog.show(getFragmentManager(), EditNameDialog.TAG);
427 return true;
428 }
429 case R.id.action_remove_file: {
430 int messageStringId = R.string.confirmation_remove_alert;
431 int posBtnStringId = R.string.confirmation_remove_remote;
432 int neuBtnStringId = -1;
433 if (mTargetFile.isFolder()) {
434 messageStringId = R.string.confirmation_remove_folder_alert;
435 posBtnStringId = R.string.confirmation_remove_remote_and_local;
436 neuBtnStringId = R.string.confirmation_remove_folder_local;
437 } else if (mTargetFile.isDown()) {
438 posBtnStringId = R.string.confirmation_remove_remote_and_local;
439 neuBtnStringId = R.string.confirmation_remove_local;
440 }
441 ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
442 messageStringId,
443 new String[]{mTargetFile.getFileName()},
444 posBtnStringId,
445 neuBtnStringId,
446 R.string.common_cancel);
447 confDialog.setOnConfirmationListener(this);
448 confDialog.show(getFragmentManager(), FileDetailFragment.FTAG_CONFIRMATION);
449 return true;
450 }
451 case R.id.action_sync_file: {
452 Account account = AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity());
453 RemoteOperation operation = new SynchronizeFileOperation(
454 mTargetFile,
455 null,
456 ((FileActivity)getSherlockActivity()).getStorageManager(),
457 account,
458 true,
459 getSherlockActivity());
460 operation.execute(account, getSherlockActivity(), mContainerActivity, mHandler, getSherlockActivity());
461 ((FileActivity) getSherlockActivity()).showLoadingDialog();
462 return true;
463 }
464 case R.id.action_cancel_download: {
465 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
466 Account account = AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity());
467 if (downloaderBinder != null && downloaderBinder.isDownloading(account, mTargetFile)) {
468 downloaderBinder.cancel(account, mTargetFile);
469 listDirectory();
470 mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
471 }
472 return true;
473 }
474 case R.id.action_cancel_upload: {
475 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
476 Account account = AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity());
477 if (uploaderBinder != null && uploaderBinder.isUploading(account, mTargetFile)) {
478 uploaderBinder.cancel(account, mTargetFile);
479 listDirectory();
480 mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
481 }
482 return true;
483 }
484 case R.id.action_see_details: {
485 ((FileFragment.ContainerActivity)getSherlockActivity()).showDetails(mTargetFile);
486 return true;
487 }
488 case R.id.action_send_file: {
489 // Obtain the file
490 if (!mTargetFile.isDown()) { // Download the file
491 Log_OC.d(TAG, mTargetFile.getRemotePath() + " : File must be downloaded");
492 mContainerActivity.startDownloadForSending(mTargetFile);
493
494 } else {
495
496 FileActivity activity = (FileActivity) getSherlockActivity();
497 activity.getFileOperationsHelper().sendDownloadedFile(mTargetFile, activity);
498 }
499 return true;
500 }
501 default:
502 return super.onContextItemSelected(item);
503 }
504 }
505
506
507 /**
508 * Use this to query the {@link OCFile} that is currently
509 * being displayed by this fragment
510 * @return The currently viewed OCFile
511 */
512 public OCFile getCurrentFile(){
513 return mFile;
514 }
515
516 /**
517 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
518 */
519 public void listDirectory(){
520 listDirectory(null);
521 }
522
523 /**
524 * Lists the given directory on the view. When the input parameter is null,
525 * it will either refresh the last known directory. list the root
526 * if there never was a directory.
527 *
528 * @param directory File to be listed
529 */
530 public void listDirectory(OCFile directory) {
531 FileDataStorageManager storageManager = ((FileActivity)getSherlockActivity()).getStorageManager();
532 if (storageManager != null) {
533
534 // Check input parameters for null
535 if(directory == null){
536 if(mFile != null){
537 directory = mFile;
538 } else {
539 directory = storageManager.getFileByPath("/");
540 if (directory == null) return; // no files, wait for sync
541 }
542 }
543
544
545 // If that's not a directory -> List its parent
546 if(!directory.isFolder()){
547 Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
548 directory = storageManager.getFileById(directory.getParentId());
549 }
550
551 swapDirectory(directory.getFileId(), storageManager);
552
553 if (mFile == null || !mFile.equals(directory)) {
554 ((ExtendedListView) getListView()).setSelectionFromTop(0, 0);
555 }
556 mFile = directory;
557 }
558 }
559
560
561 /**
562 * Change the adapted directory for a new one
563 * @param folder New file to adapt. Can be NULL, meaning "no content to adapt".
564 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
565 */
566 public void swapDirectory(long parentId, FileDataStorageManager updatedStorageManager) {
567 FileDataStorageManager storageManager = null;
568 if (updatedStorageManager != null && updatedStorageManager != storageManager) {
569 storageManager = updatedStorageManager;
570 }
571 Cursor newCursor = null;
572 if (storageManager != null) {
573 mAdapter.setStorageManager(storageManager);
574 mCursorLoader.setParentId(parentId);
575 newCursor = mCursorLoader.loadInBackground();//storageManager.getContent(folder.getFileId());
576 Uri uri = Uri.withAppendedPath(
577 ProviderTableMeta.CONTENT_URI_DIR,
578 String.valueOf(parentId));
579 Log_OC.d(TAG, "swapDirectory Uri " + uri);
580 //newCursor.setNotificationUri(getSherlockActivity().getContentResolver(), uri);
581
582 }
583 Cursor oldCursor = mAdapter.swapCursor(newCursor);
584 if (oldCursor != null){
585 oldCursor.close();
586 }
587 mAdapter.notifyDataSetChanged();
588 }
589
590
591 /**
592 * Interface to implement by any Activity that includes some instance of FileListFragment
593 *
594 * @author David A. Velasco
595 */
596 public interface ContainerActivity extends TransferServiceGetter, OnRemoteOperationListener {
597
598 /**
599 * Callback method invoked when a the user browsed into a different folder through the list of files
600 *
601 * @param file
602 */
603 public void onBrowsedDownTo(OCFile folder);
604
605 public void startDownloadForPreview(OCFile file);
606
607 public void startMediaPreview(OCFile file, int i, boolean b);
608
609 public void startImagePreview(OCFile file);
610
611 public void startSyncFolderOperation(OCFile folder);
612
613 /**
614 * Callback method invoked when a the 'transfer state' of a file changes.
615 *
616 * This happens when a download or upload is started or ended for a file.
617 *
618 * This method is necessary by now to update the user interface of the double-pane layout in tablets
619 * because methods {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and {@link FileUploaderBinder#isUploading(Account, OCFile)}
620 * won't provide the needed response before the method where this is called finishes.
621 *
622 * TODO Remove this when the transfer state of a file is kept in the database (other thing TODO)
623 *
624 * @param file OCFile which state changed.
625 * @param downloading Flag signaling if the file is now downloading.
626 * @param uploading Flag signaling if the file is now uploading.
627 */
628 public void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading);
629
630 public void startDownloadForSending(OCFile file);
631
632 }
633
634
635 @Override
636 public void onDismiss(EditNameDialog dialog) {
637 if (dialog.getResult()) {
638 String newFilename = dialog.getNewFilename();
639 Log_OC.d(TAG, "name edit dialog dismissed with new name " + newFilename);
640 RemoteOperation operation =
641 new RenameFileOperation(
642 mTargetFile,
643 AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()),
644 newFilename,
645 ((FileActivity)getSherlockActivity()).getStorageManager());
646 operation.execute(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity, mHandler, getSherlockActivity());
647 ((FileActivity) getSherlockActivity()).showLoadingDialog();
648 }
649 }
650
651
652 @Override
653 public void onConfirmation(String callerTag) {
654 if (callerTag.equals(FileDetailFragment.FTAG_CONFIRMATION)) {
655 FileDataStorageManager storageManager =
656 ((FileActivity)getSherlockActivity()).getStorageManager();
657 if (storageManager.getFileById(mTargetFile.getFileId()) != null) {
658 RemoteOperation operation = new RemoveFileOperation( mTargetFile,
659 true,
660 storageManager);
661 operation.execute(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity, mHandler, getSherlockActivity());
662
663 ((FileActivity) getSherlockActivity()).showLoadingDialog();
664 }
665 }
666 }
667
668 @Override
669 public void onNeutral(String callerTag) {
670 ((FileActivity)getSherlockActivity()).getStorageManager().removeFile(mTargetFile, false, true); // TODO perform in background task / new thread
671 listDirectory();
672 mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
673 }
674
675 @Override
676 public void onCancel(String callerTag) {
677 Log_OC.d(TAG, "REMOVAL CANCELED");
678 }
679
680 /***
681 * LoaderManager.LoaderCallbacks<Cursor>
682 */
683
684 /**
685 * Instantiate and return a new Loader for the given ID. This is where the cursor is created.
686 */
687 @Override
688 public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
689 Log_OC.d(TAG, "onCreateLoader start");
690 mCursorLoader = new FileListCursorLoader((FileActivity)getSherlockActivity(),
691 ((FileActivity)getSherlockActivity()).getStorageManager());
692 if (mFile != null) {
693 mCursorLoader.setParentId(mFile.getFileId());
694 } else {
695 mCursorLoader.setParentId(1);
696 }
697 Log_OC.d(TAG, "onCreateLoader end");
698 return mCursorLoader;
699 }
700
701
702 /**
703 * Called when a previously created loader has finished its load. Here, you can start using the cursor.
704 */
705 @Override
706 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
707 Log_OC.d(TAG, "onLoadFinished start");
708
709 FileDataStorageManager storageManager = ((FileActivity)getSherlockActivity()).getStorageManager();
710 if (storageManager != null) {
711 mCursorLoader.setStorageManager(storageManager);
712 if (mFile != null) {
713 mCursorLoader.setParentId(mFile.getFileId());
714 } else {
715 mCursorLoader.setParentId(1);
716 }
717 mAdapter.swapCursor(mCursorLoader.loadInBackground());
718 }
719
720 // if(mAdapter != null && cursor != null)
721 // mAdapter.swapCursor(cursor); //swap the new cursor in.
722 // else
723 // Log_OC.d(TAG,"OnLoadFinished: mAdapter is null");
724
725 Log_OC.d(TAG, "onLoadFinished end");
726 }
727
728
729 /**
730 * Called when a previously created loader is being reset, thus making its data unavailable.
731 * It is being reset in order to create a new cursor to query different data.
732 * This is called when the last Cursor provided to onLoadFinished() above is about to be closed.
733 * We need to make sure we are no longer using it.
734 */
735 @Override
736 public void onLoaderReset(Loader<Cursor> loader) {
737 Log_OC.d(TAG, "onLoadReset start");
738 if(mAdapter != null)
739 mAdapter.swapCursor(null);
740 else
741 Log_OC.d(TAG,"OnLoadFinished: mAdapter is null");
742 Log_OC.d(TAG, "onLoadReset end");
743 }
744
745
746 }