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