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