android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/common_cancel"
+ android:layout_marginBottom="15dp"
+ />
+
+ <ImageView
+ android:id="@+id/error_image"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_margin="0dp"
+ android:layout_gravity="center_horizontal"
+ android:contentDescription="@string/downloader_download_failed_ticker"
+ android:src="@drawable/image_fail" />
+
+ <TextView
+ android:id="@+id/errorText"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_horizontal"
+ android:layout_margin="40dp"
+ android:text="@string/downloader_download_failed_ticker"
/>
-
+
</LinearLayout>
public static final String EXTRA_FILE = "FILE";
public static final String EXTRA_ACCOUNT = "ACCOUNT";
+ private static final String EXTRA_ERROR = "ERROR";
private FileFragment.ContainerActivity mContainerActivity;
private boolean mListening;
private static final String TAG = FileDownloadFragment.class.getSimpleName();
-
+
private boolean mIgnoreFirstSavedState;
+ private boolean mError;
/**
mProgressListener = null;
mListening = false;
mIgnoreFirstSavedState = false;
+ mError = false;
}
mProgressListener = null;
mListening = false;
mIgnoreFirstSavedState = ignoreFirstSavedState;
+ mError = false;
}
if (!mIgnoreFirstSavedState) {
mFile = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_FILE);
mAccount = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_ACCOUNT);
+ mError = savedInstanceState.getBoolean(FileDownloadFragment.EXTRA_ERROR);
} else {
mIgnoreFirstSavedState = false;
}
((Button)mView.findViewById(R.id.cancelBtn)).setOnClickListener(this);
+ if (mError) {
+ setButtonsForRemote();
+ } else {
+ setButtonsForTransferring();
+ }
+
return view;
}
super.onSaveInstanceState(outState);
outState.putParcelable(FileDownloadFragment.EXTRA_FILE, mFile);
outState.putParcelable(FileDownloadFragment.EXTRA_ACCOUNT, mAccount);
+ outState.putBoolean(FileDownloadFragment.EXTRA_ERROR, mError);
}
@Override
*/
public void updateView(boolean transferring) {
// configure UI for depending upon local state of the file
- FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
+ FileDownloaderBinder downloaderBinder = (mContainerActivity == null) ? null : mContainerActivity.getFileDownloaderBinder();
if (transferring || (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile))) {
setButtonsForTransferring();
setButtonsForRemote();
}
getView().invalidate();
+
}
* Enables or disables buttons for a file being downloaded
*/
private void setButtonsForTransferring() {
- Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
- downloadButton.setText(R.string.common_cancel);
+ getView().findViewById(R.id.cancelBtn).setVisibility(View.VISIBLE);
// show the progress bar for the transfer
- ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progressBar);
- progressBar.setVisibility(View.VISIBLE);
+ getView().findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
TextView progressText = (TextView)getView().findViewById(R.id.progressText);
progressText.setText(R.string.downloader_download_in_progress_ticker);
+ progressText.setVisibility(View.VISIBLE);
+
+ // hides the error icon
+ getView().findViewById(R.id.errorText).setVisibility(View.GONE);
+ getView().findViewById(R.id.error_image).setVisibility(View.GONE);
}
* Enables or disables buttons for a file locally available
*/
private void setButtonsForDown() {
- Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
- downloadButton.setVisibility(View.GONE);
+ getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE);
// hides the progress bar
- ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progressBar);
- progressBar.setVisibility(View.GONE);
+ getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
// updates the text message
TextView progressText = (TextView)getView().findViewById(R.id.progressText);
progressText.setText(R.string.common_loading);
+ progressText.setVisibility(View.VISIBLE);
+
+ // hides the error icon
+ getView().findViewById(R.id.errorText).setVisibility(View.GONE);
+ getView().findViewById(R.id.error_image).setVisibility(View.GONE);
}
/**
* Enables or disables buttons for a file not locally available
+ *
+ * Currently, this is only used when a download was failed
*/
private void setButtonsForRemote() {
- Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
- downloadButton.setVisibility(View.GONE);
- //downloadButton.setText(R.string.filedetails_download);
-
- // hides the progress bar
- ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progressBar);
- progressBar.setVisibility(View.GONE);
+ getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE);
- // updates the text message
- TextView progressText = (TextView)getView().findViewById(R.id.progressText);
- progressText.setText(R.string.downloader_not_downloaded_yet);
+ // hides the progress bar and message
+ getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
+ getView().findViewById(R.id.progressText).setVisibility(View.GONE);
+
+ // shows the error icon and message
+ getView().findViewById(R.id.errorText).setVisibility(View.VISIBLE);
+ getView().findViewById(R.id.error_image).setVisibility(View.VISIBLE);
}
mLastPercent = percent;
}
+ }
+
+
+ public void setError(boolean error) {
+ mError = error;
};
OCFile currentFile = mPreviewImagePagerAdapter.getFileAt(position);
getSupportActionBar().setTitle(currentFile.getFileName());
if (!currentFile.isDown()) {
- requestForDownload(currentFile);
- /*} else {
- FileFragment fragment = mPreviewImagePagerAdapter.getFragmentAt(mViewPager.getCurrentItem());
- if (fragment instanceof PreviewImageFragment) {
- ((PreviewImageFragment)fragment).showError();
- }*/
+ if (!mPreviewImagePagerAdapter.pendingErrorAt(position)) {
+ requestForDownload(currentFile);
+ }
}
}
}
}
- private void updateCurrentDownloadFragment(boolean transferring) {
- FileFragment fragment = mPreviewImagePagerAdapter.getFragmentAt(mViewPager.getCurrentItem());
- if (fragment instanceof FileDownloadFragment) {
- ((FileDownloadFragment) fragment).updateView(transferring);
- //mViewPager.invalidate();
- }
- }
-
-
/**
* Class waiting for broadcast events from the {@link FielDownloader} service.
*
OCFile file = mStorageManager.getFileByPath(downloadedRemotePath);
int position = mPreviewImagePagerAdapter.getFilePosition(file);
boolean downloadWasFine = intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false);
- boolean isCurrent = (mViewPager.getCurrentItem() == position);
+ //boolean isOffscreen = Math.abs((mViewPager.getCurrentItem() - position)) <= mViewPager.getOffscreenPageLimit();
if (position >= 0) {
- /// ITS MY BUSSINESS
if (downloadWasFine) {
- mPreviewImagePagerAdapter.updateFile(position, file);
- mPreviewImagePagerAdapter.notifyDataSetChanged();
+ mPreviewImagePagerAdapter.updateFile(position, file);
- } else if (isCurrent) {
- updateCurrentDownloadFragment(false);
+ } else {
+ mPreviewImagePagerAdapter.updateWithDownloadError(position);
}
+ mPreviewImagePagerAdapter.notifyDataSetChanged(); // will trigger the creation of new fragments
} else {
- Log.e(TAG, "DOWNLOADED FILE NOT FOUND IN ADAPTER ");
+ Log.d(TAG, "Download finished, but the fragment is offscreen");
}
}
package com.owncloud.android.ui.preview;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.HashSet;
+import java.util.Map;
import java.util.Set;
import java.util.Vector;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
+import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
private Account mAccount;
private Set<Object> mObsoleteFragments;
private Set<Integer> mObsoletePositions;
+ private Set<Integer> mDownloadErrors;
private DataStorageManager mStorageManager;
+ private Map<Integer, FileFragment> mCachedFragments;
+
/*
private final FragmentManager mFragmentManager;
private FragmentTransaction mCurTransaction = null;
mImageFiles = mStorageManager.getDirectoryImages(parentFolder);
mObsoleteFragments = new HashSet<Object>();
mObsoletePositions = new HashSet<Integer>();
+ mDownloadErrors = new HashSet<Integer>();
//mFragmentManager = fragmentManager;
+ mCachedFragments = new HashMap<Integer, FileFragment>();
}
Fragment fragment = null;
if (file.isDown()) {
fragment = new PreviewImageFragment(file, mAccount, mObsoletePositions.contains(Integer.valueOf(i)));
+
+ } else if (mDownloadErrors.contains(Integer.valueOf(i))) {
+ fragment = new FileDownloadFragment(file, mAccount, true);
+ ((FileDownloadFragment)fragment).setError(true);
+ mDownloadErrors.remove(Integer.valueOf(i));
+
} else {
fragment = new FileDownloadFragment(file, mAccount, mObsoletePositions.contains(Integer.valueOf(i)));
}
+ mObsoletePositions.remove(Integer.valueOf(i));
return fragment;
}
return mImageFiles.get(position).getFileName();
}
+
public void updateFile(int position, OCFile file) {
- mObsoleteFragments.add(instantiateItem(null, position));
+ FileFragment fragmentToUpdate = mCachedFragments.get(Integer.valueOf(position));
+ if (fragmentToUpdate != null) {
+ mObsoleteFragments.add(fragmentToUpdate);
+ }
mObsoletePositions.add(Integer.valueOf(position));
mImageFiles.set(position, file);
}
+
+ public void updateWithDownloadError(int position) {
+ FileFragment fragmentToUpdate = mCachedFragments.get(Integer.valueOf(position));
+ if (fragmentToUpdate != null) {
+ mObsoleteFragments.add(fragmentToUpdate);
+ }
+ mDownloadErrors.add(Integer.valueOf(position));
+ }
+
+ public void clearErrorAt(int position) {
+ FileFragment fragmentToUpdate = mCachedFragments.get(Integer.valueOf(position));
+ if (fragmentToUpdate != null) {
+ mObsoleteFragments.add(fragmentToUpdate);
+ }
+ mDownloadErrors.remove(Integer.valueOf(position));
+ }
+
+
@Override
public int getItemPosition(Object object) {
if (mObsoleteFragments.contains(object)) {
}
- /**
- * Should not be used for not already started fragments...
- *
- * @return
- */
- protected FileFragment getFragmentAt(int position) {
- try {
- return (FileFragment) instantiateItem(null, position);
-
- } catch (Exception e) {
- return null;
- }
+ @Override
+ public Object instantiateItem(ViewGroup container, int position) {
+ Object fragment = super.instantiateItem(container, position);
+ mCachedFragments.put(Integer.valueOf(position), (FileFragment)fragment);
+ return fragment;
+ }
+
+ @Override
+ public void destroyItem(ViewGroup container, int position, Object object) {
+ mCachedFragments.remove(Integer.valueOf(position));
+ super.destroyItem(container, position, object);
}
+ public boolean pendingErrorAt(int position) {
+ return mDownloadErrors.contains(Integer.valueOf(position));
+ }
+
+
+
/* -*
* Called when a change in the shown pages is going to start being made.
*