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 android.os.IBinder;\r
+import android.support.v4.app.Fragment;\r
import android.support.v4.app.FragmentTransaction;\r
import android.util.Log;\r
\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
* on.\r
* \r
* @author Bartek Przybylski\r
- * \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
public static final String TAG = FileDetailActivity.class.getSimpleName();\r
\r
+ public static final String EXTRA_MODE = "MODE";\r
+ public static final int MODE_DETAILS = 0;\r
+ public static final int MODE_PREVIEW = 1;\r
+\r
+ public static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";\r
+ \r
private boolean mConfigurationChangedToLandscape = false;\r
private FileDownloaderBinder mDownloaderBinder = null;\r
private ServiceConnection mDownloadConnection, mUploadConnection = null;\r
private FileUploaderBinder mUploaderBinder = null;\r
+ private boolean mWaitingToPreview;\r
+ \r
+ private OCFile mFile;\r
+ private Account mAccount;\r
\r
+ private FileDataStorageManager mStorageManager;\r
+ private DownloadFinishReceiver mDownloadFinishReceiver;\r
+ \r
\r
@Override\r
protected void onCreate(Bundle savedInstanceState) {\r
super.onCreate(savedInstanceState);\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
mConfigurationChangedToLandscape = (conf.orientation == Configuration.ORIENTATION_LANDSCAPE && \r
);\r
\r
if (!mConfigurationChangedToLandscape) {\r
- mDownloadConnection = new DetailsServiceConnection();\r
- bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);\r
- mUploadConnection = new DetailsServiceConnection();\r
- bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);\r
- \r
setContentView(R.layout.file_activity_details);\r
\r
ActionBar actionBar = getSupportActionBar();\r
actionBar.setDisplayHomeAsUpEnabled(true);\r
\r
- OCFile file = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);\r
- Account account = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);\r
- FileDetailFragment mFileDetail = new FileDetailFragment(file, account);\r
- \r
- FragmentTransaction ft = getSupportFragmentManager().beginTransaction();\r
- ft.replace(R.id.fragment, mFileDetail, FileDetailFragment.FTAG);\r
- ft.commit();\r
+ if (savedInstanceState == null) {\r
+ mWaitingToPreview = false;\r
+ createChildFragment();\r
+ } else {\r
+ mWaitingToPreview = savedInstanceState.getBoolean(KEY_WAITING_TO_PREVIEW);\r
+ }\r
+ \r
+ mDownloadConnection = new DetailsServiceConnection();\r
+ bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);\r
+ mUploadConnection = new DetailsServiceConnection();\r
+ bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);\r
+ \r
\r
} else {\r
backToDisplayActivity(); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;\r
\r
\r
}\r
+\r
+ /**\r
+ * Creates the proper fragment depending upon the state of the handled {@link OCFile} and\r
+ * the requested {@link Intent}.\r
+ */\r
+ private void createChildFragment() {\r
+ int mode = getIntent().getIntExtra(EXTRA_MODE, MODE_PREVIEW); \r
+ \r
+ Fragment newFragment = null;\r
+ if (PreviewMediaFragment.canBePreviewed(mFile) && mode == MODE_PREVIEW) {\r
+ if (mFile.isDown()) {\r
+ newFragment = new PreviewMediaFragment(mFile, mAccount);\r
+ \r
+ } else {\r
+ newFragment = new FileDetailFragment(mFile, mAccount);\r
+ mWaitingToPreview = true;\r
+ }\r
+ \r
+ } else {\r
+ newFragment = new FileDetailFragment(mFile, mAccount);\r
+ }\r
+ FragmentTransaction ft = getSupportFragmentManager().beginTransaction();\r
+ ft.replace(R.id.fragment, newFragment, FileDetailFragment.FTAG);\r
+ ft.commit();\r
+ }\r
+ \r
+\r
+ @Override\r
+ protected void onSaveInstanceState(Bundle outState) {\r
+ super.onSaveInstanceState(outState);\r
+ outState.putBoolean(KEY_WAITING_TO_PREVIEW, mWaitingToPreview);\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
\r
@Override\r
public void onServiceConnected(ComponentName component, IBinder service) {\r
+ \r
if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {\r
Log.d(TAG, "Download service connected");\r
mDownloaderBinder = (FileDownloaderBinder) service;\r
+ if (mWaitingToPreview) {\r
+ requestForDownload();\r
+ }\r
+ \r
} else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {\r
Log.d(TAG, "Upload service connected");\r
mUploaderBinder = (FileUploaderBinder) service;\r
} else {\r
return;\r
}\r
- FileDetailFragment fragment = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
- if (fragment != null)\r
- fragment.updateFileDetails(false); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())\r
+ \r
+ Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
+ FileDetailFragment detailsFragment = (fragment instanceof FileDetailFragment) ? (FileDetailFragment) fragment : null;\r
+ if (detailsFragment != null) {\r
+ detailsFragment.listenForTransferProgress();\r
+ detailsFragment.updateFileDetails(mWaitingToPreview, false); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())\r
+ }\r
}\r
\r
@Override\r
}\r
}; \r
\r
-\r
+ \r
@Override\r
public void onDestroy() {\r
super.onDestroy();\r
\r
\r
\r
- @Override\r
- protected void onResume() {\r
- \r
- super.onResume();\r
- if (!mConfigurationChangedToLandscape) { \r
- FileDetailFragment fragment = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);\r
- fragment.updateFileDetails(false);\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
- intent.putExtra(FileDetailFragment.EXTRA_FILE, getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE));\r
- intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT));\r
+ OCFile targetFile = null;\r
+ if (mFile != null) {\r
+ targetFile = mStorageManager.getFileById(mFile.getParentId());\r
+ }\r
+ intent.putExtra(FileDetailFragment.EXTRA_FILE, targetFile);\r
+ intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);\r
startActivity(intent);\r
finish();\r
}\r
public FileUploaderBinder getFileUploaderBinder() {\r
return mUploaderBinder;\r
}\r
+\r
+\r
+ @Override\r
+ public void showFragmentWithDetails(OCFile file) {\r
+ FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r
+ transaction.replace(R.id.fragment, new FileDetailFragment(file, mAccount), FileDetailFragment.FTAG); \r
+ transaction.commit();\r
+ }\r
+\r
+ \r
+ private void requestForDownload() {\r
+ if (!mDownloaderBinder.isDownloading(mAccount, mFile)) {\r
+ Intent i = new Intent(this, FileDownloader.class);\r
+ i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);\r
+ i.putExtra(FileDownloader.EXTRA_FILE, mFile);\r
+ startService(i);\r
+ }\r
+ }\r
+\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
+ 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