Automatic download of previewable files in download mode

This commit is contained in:
David A. Velasco 2013-02-22 16:39:58 +01:00
parent 559ab6566b
commit 0c75a839f2
5 changed files with 87 additions and 25 deletions

View file

@ -479,8 +479,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
*/ */
private void sendBroadcastNewDownload(DownloadFileOperation download) { private void sendBroadcastNewDownload(DownloadFileOperation download) {
Intent added = new Intent(DOWNLOAD_ADDED_MESSAGE); Intent added = new Intent(DOWNLOAD_ADDED_MESSAGE);
/*added.putExtra(ACCOUNT_NAME, download.getAccount().name); added.putExtra(ACCOUNT_NAME, download.getAccount().name);
added.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());*/ added.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());
added.putExtra(EXTRA_FILE_PATH, download.getSavePath()); added.putExtra(EXTRA_FILE_PATH, download.getSavePath());
sendStickyBroadcast(added); sendStickyBroadcast(added);
} }

View file

@ -61,7 +61,7 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
public static final int MODE_DETAILS = 0; public static final int MODE_DETAILS = 0;
public static final int MODE_PREVIEW = 1; public static final int MODE_PREVIEW = 1;
private static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW"; public static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
private boolean mConfigurationChangedToLandscape = false; private boolean mConfigurationChangedToLandscape = false;
private FileDownloaderBinder mDownloaderBinder = null; private FileDownloaderBinder mDownloaderBinder = null;
@ -303,12 +303,14 @@ public class FileDetailActivity extends SherlockFragmentActivity implements File
} }
@Override @Override
public void notifySuccessfulDownload(OCFile file) { public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success) {
if (mWaitingToPreview) { if (success) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); if (mWaitingToPreview) {
transaction.replace(R.id.fragment, new FilePreviewFragment(file, mAccount), FileDetailFragment.FTAG); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.commit(); transaction.replace(R.id.fragment, new FilePreviewFragment(file, mAccount), FileDetailFragment.FTAG);
mWaitingToPreview = false; transaction.commit();
mWaitingToPreview = false;
}
} }
} }

View file

@ -140,6 +140,10 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
private static int[] mMenuIdentifiersToPatch = {R.id.action_about_app}; private static int[] mMenuIdentifiersToPatch = {R.id.action_about_app};
private OCFile mWaitingToPreview;
private Handler mHandler;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
Log.d(getClass().toString(), "onCreate() start"); Log.d(getClass().toString(), "onCreate() start");
@ -155,6 +159,10 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
if(savedInstanceState != null) { if(savedInstanceState != null) {
// TODO - test if savedInstanceState should take precedence over file in the intent ALWAYS (now), NEVER, or SOME TIMES // TODO - test if savedInstanceState should take precedence over file in the intent ALWAYS (now), NEVER, or SOME TIMES
mCurrentDir = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_FILE); mCurrentDir = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_FILE);
mWaitingToPreview = (OCFile) savedInstanceState.getParcelable(FileDetailActivity.KEY_WAITING_TO_PREVIEW);
} else {
mWaitingToPreview = null;
} }
if (!AccountUtils.accountsAreSetup(this)) { if (!AccountUtils.accountsAreSetup(this)) {
@ -293,8 +301,13 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
if (mDualPane && getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG) == null) { if (mDualPane && getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG) == null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (mCurrentFile != null) { if (mCurrentFile != null) {
if (mCurrentFile.isDown() && FilePreviewFragment.canBePreviewed(mCurrentFile)) { if (FilePreviewFragment.canBePreviewed(mCurrentFile)) {
transaction.replace(R.id.file_details_container, new FilePreviewFragment(mCurrentFile, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG); if (mCurrentFile.isDown()) {
transaction.replace(R.id.file_details_container, new FilePreviewFragment(mCurrentFile, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
} else {
transaction.replace(R.id.file_details_container, new FileDetailFragment(mCurrentFile, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
mWaitingToPreview = mCurrentFile;
}
} else { } else {
transaction.replace(R.id.file_details_container, new FileDetailFragment(mCurrentFile, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG); transaction.replace(R.id.file_details_container, new FileDetailFragment(mCurrentFile, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
} }
@ -542,6 +555,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
} }
} }
} }
outState.putParcelable(FileDetailActivity.KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
Log.d(getClass().toString(), "onSaveInstanceState() end"); Log.d(getClass().toString(), "onSaveInstanceState() end");
} }
@ -571,7 +585,8 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
registerReceiver(mUploadFinishReceiver, uploadIntentFilter); registerReceiver(mUploadFinishReceiver, uploadIntentFilter);
// Listen for download messages // Listen for download messages
IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE); IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_ADDED_MESSAGE);
//downloadIntentFilter.addAction(FileDownloader.DOWNLOAD_FINISH_MESSAGE);
mDownloadFinishReceiver = new DownloadFinishReceiver(); mDownloadFinishReceiver = new DownloadFinishReceiver();
registerReceiver(mDownloadFinishReceiver, downloadIntentFilter); registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
@ -973,12 +988,27 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH); String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME); String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
boolean sameAccount = accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name); if (accountName != null && AccountUtils.getCurrentOwnCloudAccount(context) != null) {
boolean isDescendant = (mCurrentDir != null) && (downloadedRemotePath != null) && (downloadedRemotePath.startsWith(mCurrentDir.getRemotePath())); boolean sameAccount = accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name);
if (sameAccount && isDescendant) { boolean isDescendant = (mCurrentDir != null) && (downloadedRemotePath != null) && (downloadedRemotePath.startsWith(mCurrentDir.getRemotePath()));
OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList); if (sameAccount && isDescendant) {
if (fileListFragment != null) { OCFileListFragment fileListFragment = (OCFileListFragment) getSupportFragmentManager().findFragmentById(R.id.fileList);
fileListFragment.listDirectory(); if (fileListFragment != null) {
fileListFragment.listDirectory();
if ( mWaitingToPreview != null &&
mWaitingToPreview.getRemotePath().equals(downloadedRemotePath) &&
intent.getAction().equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE) ) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
if (fragment != null && fragment instanceof FileDetailFragment ) {
FileDetailFragment detailFragment = (FileDetailFragment) fragment;
if (detailFragment.getFile().getRemotePath().equals(downloadedRemotePath)) {
detailFragment.listenForTransferProgress();
detailFragment.updateFileDetails(true);
}
}
}
}
} }
} }
} }
@ -1027,8 +1057,14 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
if (mDualPane) { if (mDualPane) {
// buttons in the details view are problematic when trying to reuse an existing fragment; create always a new one solves some of them, BUT no all; downloads are 'dangerous' // buttons in the details view are problematic when trying to reuse an existing fragment; create always a new one solves some of them, BUT no all; downloads are 'dangerous'
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (file != null && file.isDown() && FilePreviewFragment.canBePreviewed(file)) { if (file != null && FilePreviewFragment.canBePreviewed(file)) {
transaction.replace(R.id.file_details_container, new FilePreviewFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG); if (file.isDown()) {
transaction.replace(R.id.file_details_container, new FilePreviewFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
} else {
transaction.replace(R.id.file_details_container, new FileDetailFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
mWaitingToPreview = file;
requestForDownload();
}
} else { } else {
transaction.replace(R.id.file_details_container, new FileDetailFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG); transaction.replace(R.id.file_details_container, new FileDetailFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
} }
@ -1091,6 +1127,10 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
if (component.equals(new ComponentName(FileDisplayActivity.this, FileDownloader.class))) { if (component.equals(new ComponentName(FileDisplayActivity.this, FileDownloader.class))) {
Log.d(TAG, "Download service connected"); Log.d(TAG, "Download service connected");
mDownloaderBinder = (FileDownloaderBinder) service; mDownloaderBinder = (FileDownloaderBinder) service;
if (mWaitingToPreview != null) {
requestForDownload();
}
} else if (component.equals(new ComponentName(FileDisplayActivity.this, FileUploader.class))) { } else if (component.equals(new ComponentName(FileDisplayActivity.this, FileUploader.class))) {
Log.d(TAG, "Upload service connected"); Log.d(TAG, "Upload service connected");
mUploaderBinder = (FileUploaderBinder) service; mUploaderBinder = (FileUploaderBinder) service;
@ -1313,8 +1353,27 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
@Override @Override
public void notifySuccessfulDownload(OCFile file) { public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success) {
// TODO refactoring once for all and remove this stupid method if (success) {
if (mWaitingToPreview != null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.file_details_container, new FilePreviewFragment(file, AccountUtils.getCurrentOwnCloudAccount(this)), FileDetailFragment.FTAG);
transaction.commit();
mWaitingToPreview = null;
}
}
mDownloadFinishReceiver.onReceive(this, intent);
}
private void requestForDownload() {
Account account = AccountUtils.getCurrentOwnCloudAccount(this);
if (!mDownloaderBinder.isDownloading(account, mWaitingToPreview)) {
Intent i = new Intent(this, FileDownloader.class);
i.putExtra(FileDownloader.EXTRA_ACCOUNT, account);
i.putExtra(FileDownloader.EXTRA_FILE, mWaitingToPreview);
startService(i);
}
} }

View file

@ -833,9 +833,9 @@ public class FileDetailFragment extends SherlockFragment implements
if (mFile.getRemotePath().equals(downloadedRemotePath)) { if (mFile.getRemotePath().equals(downloadedRemotePath)) {
if (downloadWasFine) { if (downloadWasFine) {
mFile = mStorageManager.getFileByPath(downloadedRemotePath); mFile = mStorageManager.getFileByPath(downloadedRemotePath);
mContainerActivity.notifySuccessfulDownload(mFile);
getActivity().removeStickyBroadcast(intent);
} }
mContainerActivity.notifySuccessfulDownload(mFile, intent, downloadWasFine);
getActivity().removeStickyBroadcast(intent);
updateFileDetails(false); // it updates the buttons; must be called although !downloadWasFine updateFileDetails(false); // it updates the buttons; must be called although !downloadWasFine
} }
} }

View file

@ -18,6 +18,7 @@
package com.owncloud.android.ui.fragment; package com.owncloud.android.ui.fragment;
import android.content.Intent;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.datamodel.OCFile;
@ -68,7 +69,7 @@ public interface FileFragment {
public void showFragmentWithDetails(OCFile file); public void showFragmentWithDetails(OCFile file);
public void notifySuccessfulDownload(OCFile file); public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success);
} }