import android.accounts.Account;\r
import android.app.Dialog;\r
import android.app.ProgressDialog;\r
+import android.content.BroadcastReceiver;\r
import android.content.ComponentName;\r
import android.content.Context;\r
import android.content.Intent;\r
+import android.content.IntentFilter;\r
import android.content.ServiceConnection;\r
import android.content.res.Configuration;\r
import android.os.Bundle;\r
import com.actionbarsherlock.app.ActionBar;\r
import com.actionbarsherlock.app.SherlockFragmentActivity;\r
import com.actionbarsherlock.view.MenuItem;\r
+import com.owncloud.android.datamodel.FileDataStorageManager;\r
import com.owncloud.android.datamodel.OCFile;\r
import com.owncloud.android.files.services.FileDownloader;\r
import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;\r
import com.owncloud.android.files.services.FileUploader;\r
import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;\r
import com.owncloud.android.ui.fragment.FileDetailFragment;\r
+import com.owncloud.android.ui.fragment.FileFragment;\r
import com.owncloud.android.ui.preview.PreviewMediaFragment;\r
\r
+import com.owncloud.android.AccountUtils;\r
import com.owncloud.android.R;\r
\r
/**\r
* @author Bartek Przybylski\r
* @author David A. Velasco\r
*/\r
-public class FileDetailActivity extends SherlockFragmentActivity implements FileDetailFragment.ContainerActivity {\r
+public class FileDetailActivity extends SherlockFragmentActivity implements FileFragment.ContainerActivity {\r
\r
public static final int DIALOG_SHORT_WAIT = 0;\r
\r
\r
private OCFile mFile;\r
private Account mAccount;\r
+\r
+ private FileDataStorageManager mStorageManager;\r
+ private DownloadFinishReceiver mDownloadFinishReceiver;\r
\r
\r
@Override\r
\r
mFile = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);\r
mAccount = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);\r
+ mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());\r
\r
// check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity \r
Configuration conf = getResources().getConfiguration();\r
super.onSaveInstanceState(outState);\r
outState.putBoolean(KEY_WAITING_TO_PREVIEW, mWaitingToPreview);\r
}\r
-\r
-\r
+ \r
+ \r
+ @Override\r
+ public void onPause() {\r
+ super.onPause();\r
+ if (mDownloadFinishReceiver != null) {\r
+ unregisterReceiver(mDownloadFinishReceiver);\r
+ mDownloadFinishReceiver = null;\r
+ }\r
+ }\r
+ \r
+ \r
+ @Override\r
+ public void onResume() {\r
+ super.onResume();\r
+ if (!mConfigurationChangedToLandscape) {\r
+ // TODO this is probably unnecessary\r
+ Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
+ if (fragment != null && fragment instanceof FileDetailFragment) {\r
+ ((FileDetailFragment) fragment).updateFileDetails(false, false);\r
+ }\r
+ }\r
+ // Listen for download messages\r
+ IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_ADDED_MESSAGE);\r
+ downloadIntentFilter.addAction(FileDownloader.DOWNLOAD_FINISH_MESSAGE);\r
+ mDownloadFinishReceiver = new DownloadFinishReceiver();\r
+ registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);\r
+ }\r
+ \r
+ \r
/** Defines callbacks for service binding, passed to bindService() */\r
private class DetailsServiceConnection implements ServiceConnection {\r
\r
\r
\r
\r
- @Override\r
- protected void onResume() {\r
- \r
- super.onResume();\r
- if (!mConfigurationChangedToLandscape) {\r
- Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
- if (fragment != null && fragment instanceof FileDetailFragment) {\r
- ((FileDetailFragment) fragment).updateFileDetails(false, false);\r
- }\r
- }\r
- }\r
- \r
-\r
private void backToDisplayActivity() {\r
Intent intent = new Intent(this, FileDisplayActivity.class);\r
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);\r
}\r
}\r
\r
- @Override\r
- public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success) {\r
- if (success) {\r
- if (mWaitingToPreview) {\r
- FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
- transaction.replace(R.id.fragment, new PreviewMediaFragment(file, mAccount), FileDetailFragment.FTAG); \r
- transaction.commit();\r
- mWaitingToPreview = false;\r
+ \r
+ /**\r
+ * Class waiting for broadcast events from the {@link FielDownloader} service.\r
+ * \r
+ * Updates the UI when a download is started or finished, provided that it is relevant for the\r
+ * current file.\r
+ */\r
+ private class DownloadFinishReceiver extends BroadcastReceiver {\r
+ @Override\r
+ public void onReceive(Context context, Intent intent) {\r
+ boolean sameAccount = isSameAccount(context, intent);\r
+ String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);\r
+ boolean samePath = (mFile != null && mFile.getRemotePath().equals(downloadedRemotePath));\r
+ \r
+ if (sameAccount && samePath) {\r
+ updateChildFragment(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false));\r
}\r
+ \r
+ removeStickyBroadcast(intent);\r
+ }\r
+\r
+ private boolean isSameAccount(Context context, Intent intent) {\r
+ String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);\r
+ return (accountName != null && accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name));\r
}\r
}\r
- \r
+\r
+\r
+ public void updateChildFragment(String downloadEvent, String downloadedRemotePath, boolean success) {\r
+ Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
+ if (fragment != null && fragment instanceof FileDetailFragment) {\r
+ FileDetailFragment detailsFragment = (FileDetailFragment) fragment;\r
+ OCFile fileInFragment = detailsFragment.getFile();\r
+ if (fileInFragment != null && !downloadedRemotePath.equals(fileInFragment.getRemotePath())) {\r
+ // this never should happen; fileInFragment should be always equals to mFile, that was compared to downloadedRemotePath in DownloadReceiver \r
+ mWaitingToPreview = false;\r
+ \r
+ } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE)) {\r
+ // grants that the progress bar is updated\r
+ detailsFragment.listenForTransferProgress();\r
+ detailsFragment.updateFileDetails(true, false);\r
+ \r
+ } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {\r
+ // refresh the details fragment \r
+ if (success && mWaitingToPreview) {\r
+ mFile = mStorageManager.getFileById(mFile.getFileId()); // update the file from database, for the local storage path\r
+ FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
+ transaction.replace(R.id.fragment, new PreviewMediaFragment(mFile, mAccount), FileDetailFragment.FTAG);\r
+ transaction.commit();\r
+ mWaitingToPreview = false;\r
+ \r
+ } else {\r
+ detailsFragment.updateFileDetails(false, (success));\r
+ // TODO error message if !success ¿?\r
+ }\r
+ }\r
+ } // TODO else if (fragment != null && fragment )\r
+ \r
+ \r
+ }\r
+\r
}\r
import com.owncloud.android.authenticator.AccountAuthenticator;\r
import com.owncloud.android.datamodel.FileDataStorageManager;\r
import com.owncloud.android.datamodel.OCFile;\r
-import com.owncloud.android.files.services.FileDownloader;\r
import com.owncloud.android.files.services.FileObserverService;\r
import com.owncloud.android.files.services.FileUploader;\r
import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;\r
public void onActivityCreated(Bundle savedInstanceState) {\r
super.onActivityCreated(savedInstanceState);\r
if (mAccount != null) {\r
- mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());;\r
+ mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());\r
}\r
}\r
\r
}\r
\r
\r
- /* *\r
- * Once the file download has finished -> update view\r
- * @author Bartek Przybylski\r
- * - /\r
- private class DownloadFinishReceiver extends BroadcastReceiver {\r
- @Override\r
- public void onReceive(Context context, Intent intent) {\r
- String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);\r
-\r
- if (!isEmpty() && accountName.equals(mAccount.name)) {\r
- boolean downloadWasFine = intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false);\r
- String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);\r
- if (mFile.getRemotePath().equals(downloadedRemotePath)) {\r
- if (downloadWasFine) {\r
- mFile = mStorageManager.getFileByPath(downloadedRemotePath);\r
- }\r
- mContainerActivity.notifySuccessfulDownload(mFile, intent, downloadWasFine);\r
- getActivity().removeStickyBroadcast(intent);\r
- updateFileDetails(false); // it updates the buttons; must be called although !downloadWasFine\r
- }\r
- }\r
- }\r
- }\r
- */\r
- \r
- \r
/**\r
* Once the file upload has finished -> update view\r
* \r