+ /**
+ * Class waiting for broadcast events from the {@link FielDownloader} service.
+ *
+ * Updates the UI when a download is started or finished, provided that it is relevant for the
+ * current file.
+ */
+ private class DownloadFinishReceiver extends BroadcastReceiver {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ boolean sameAccount = isSameAccount(context, intent);
+ String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
+ boolean samePath = (mFile != null && mFile.getRemotePath().equals(downloadedRemotePath));
+
+ if (sameAccount && samePath) {
+ updateChildFragment(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false));
+ }
+
+ removeStickyBroadcast(intent);
+ }
+
+ private boolean isSameAccount(Context context, Intent intent) {
+ String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
+ return (accountName != null && accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name));
+ }
+ }
+
+
+ public void updateChildFragment(String downloadEvent, String downloadedRemotePath, boolean success) {
+ Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
+ if (fragment != null && fragment instanceof FileDetailFragment) {
+ FileDetailFragment detailsFragment = (FileDetailFragment) fragment;
+ OCFile fileInFragment = detailsFragment.getFile();
+ if (fileInFragment != null && !downloadedRemotePath.equals(fileInFragment.getRemotePath())) {
+ // this never should happen; fileInFragment should be always equals to mFile, that was compared to downloadedRemotePath in DownloadReceiver
+ mWaitingToPreview = false;
+
+ } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE)) {
+ // grants that the progress bar is updated
+ detailsFragment.listenForTransferProgress();
+ detailsFragment.updateFileDetails(true, false);
+
+ } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {
+ // refresh the details fragment
+ if (success && mWaitingToPreview) {
+ mFile = mStorageManager.getFileById(mFile.getFileId()); // update the file from database, for the local storage path
+ FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
+ transaction.replace(R.id.fragment, new PreviewMediaFragment(mFile, mAccount), FileDetailFragment.FTAG);
+ transaction.commit();
+ mWaitingToPreview = false;
+
+ } else {
+ detailsFragment.updateFileDetails(false, (success));
+ // TODO error message if !success ¿?
+ }
+ }
+ } // TODO else if (fragment != null && fragment )
+
+ }
+