X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/75ae57b6d34e000d6a9fc89d6d767a7c258b1e32..d0767f1a4f45c5ab5ef57fa609688e2a1cc56b36:/src/com/owncloud/android/ui/activity/FileDetailActivity.java diff --git a/src/com/owncloud/android/ui/activity/FileDetailActivity.java b/src/com/owncloud/android/ui/activity/FileDetailActivity.java index bdd475aa..2ab8bcc9 100644 --- a/src/com/owncloud/android/ui/activity/FileDetailActivity.java +++ b/src/com/owncloud/android/ui/activity/FileDetailActivity.java @@ -20,9 +20,11 @@ package com.owncloud.android.ui.activity; import android.accounts.Account; import android.app.Dialog; import android.app.ProgressDialog; +import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.ServiceConnection; import android.content.res.Configuration; import android.os.Bundle; @@ -34,14 +36,17 @@ import android.util.Log; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.view.MenuItem; +import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.services.FileDownloader; import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder; import com.owncloud.android.files.services.FileUploader; import com.owncloud.android.files.services.FileUploader.FileUploaderBinder; import com.owncloud.android.ui.fragment.FileDetailFragment; +import com.owncloud.android.ui.fragment.FileFragment; import com.owncloud.android.ui.preview.PreviewMediaFragment; +import com.owncloud.android.AccountUtils; import com.owncloud.android.R; /** @@ -51,7 +56,7 @@ import com.owncloud.android.R; * @author Bartek Przybylski * @author David A. Velasco */ -public class FileDetailActivity extends SherlockFragmentActivity implements FileDetailFragment.ContainerActivity { +public class FileDetailActivity extends SherlockFragmentActivity implements FileFragment.ContainerActivity { public static final int DIALOG_SHORT_WAIT = 0; @@ -71,6 +76,9 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File private OCFile mFile; private Account mAccount; + + private FileDataStorageManager mStorageManager; + private DownloadFinishReceiver mDownloadFinishReceiver; @Override @@ -79,6 +87,7 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File mFile = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE); mAccount = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT); + mStorageManager = new FileDataStorageManager(mAccount, getContentResolver()); // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity Configuration conf = getResources().getConfiguration(); @@ -143,8 +152,36 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File super.onSaveInstanceState(outState); outState.putBoolean(KEY_WAITING_TO_PREVIEW, mWaitingToPreview); } - - + + + @Override + public void onPause() { + super.onPause(); + if (mDownloadFinishReceiver != null) { + unregisterReceiver(mDownloadFinishReceiver); + mDownloadFinishReceiver = null; + } + } + + + @Override + public void onResume() { + super.onResume(); + if (!mConfigurationChangedToLandscape) { + // TODO this is probably unnecessary + Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); + if (fragment != null && fragment instanceof FileDetailFragment) { + ((FileDetailFragment) fragment).updateFileDetails(false, false); + } + } + // Listen for download messages + IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_ADDED_MESSAGE); + downloadIntentFilter.addAction(FileDownloader.DOWNLOAD_FINISH_MESSAGE); + mDownloadFinishReceiver = new DownloadFinishReceiver(); + registerReceiver(mDownloadFinishReceiver, downloadIntentFilter); + } + + /** Defines callbacks for service binding, passed to bindService() */ private class DetailsServiceConnection implements ServiceConnection { @@ -218,19 +255,6 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File - @Override - protected void onResume() { - - super.onResume(); - if (!mConfigurationChangedToLandscape) { - Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG); - if (fragment != null && fragment instanceof FileDetailFragment) { - ((FileDetailFragment) fragment).updateFileDetails(false, false); - } - } - } - - private void backToDisplayActivity() { Intent intent = new Intent(this, FileDisplayActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); @@ -302,16 +326,65 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File } } - @Override - public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success) { - if (success) { - if (mWaitingToPreview) { - FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - transaction.replace(R.id.fragment, new PreviewMediaFragment(file, mAccount), FileDetailFragment.FTAG); - transaction.commit(); - mWaitingToPreview = false; + + /** + * 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 ) + + + } + }