From: David A. Velasco Date: Fri, 1 Mar 2013 13:20:25 +0000 (+0100) Subject: Improved message for failed downloads in gallery X-Git-Tag: oc-android-1.4.3~39^2~23 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/db7eea712def99464bab329da008c6ca56f581cf?hp=--cc Improved message for failed downloads in gallery --- db7eea712def99464bab329da008c6ca56f581cf diff --git a/res/layout/file_download_fragment.xml b/res/layout/file_download_fragment.xml index 73f9a6e4..aea16d07 100644 --- a/res/layout/file_download_fragment.xml +++ b/res/layout/file_download_fragment.xml @@ -47,7 +47,26 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/common_cancel" + android:layout_marginBottom="15dp" + /> + + + + - + diff --git a/src/com/owncloud/android/ui/preview/FileDownloadFragment.java b/src/com/owncloud/android/ui/preview/FileDownloadFragment.java index eea10cca..2279ff00 100644 --- a/src/com/owncloud/android/ui/preview/FileDownloadFragment.java +++ b/src/com/owncloud/android/ui/preview/FileDownloadFragment.java @@ -52,6 +52,7 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis 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; @@ -64,8 +65,9 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis private boolean mListening; private static final String TAG = FileDownloadFragment.class.getSimpleName(); - + private boolean mIgnoreFirstSavedState; + private boolean mError; /** @@ -80,6 +82,7 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis mProgressListener = null; mListening = false; mIgnoreFirstSavedState = false; + mError = false; } @@ -99,6 +102,7 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis mProgressListener = null; mListening = false; mIgnoreFirstSavedState = ignoreFirstSavedState; + mError = false; } @@ -117,6 +121,7 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis if (!mIgnoreFirstSavedState) { mFile = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_FILE); mAccount = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_ACCOUNT); + mError = savedInstanceState.getBoolean(FileDownloadFragment.EXTRA_ERROR); } else { mIgnoreFirstSavedState = false; } @@ -131,6 +136,12 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis ((Button)mView.findViewById(R.id.cancelBtn)).setOnClickListener(this); + if (mError) { + setButtonsForRemote(); + } else { + setButtonsForTransferring(); + } + return view; } @@ -167,6 +178,7 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis super.onSaveInstanceState(outState); outState.putParcelable(FileDownloadFragment.EXTRA_FILE, mFile); outState.putParcelable(FileDownloadFragment.EXTRA_ACCOUNT, mAccount); + outState.putBoolean(FileDownloadFragment.EXTRA_ERROR, mError); } @Override @@ -249,7 +261,7 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis */ 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(); @@ -261,6 +273,7 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis setButtonsForRemote(); } getView().invalidate(); + } @@ -268,14 +281,17 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis * 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); } @@ -283,34 +299,37 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis * 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); } @@ -366,6 +385,11 @@ public class FileDownloadFragment extends SherlockFragment implements OnClickLis mLastPercent = percent; } + } + + + public void setError(boolean error) { + mError = error; }; diff --git a/src/com/owncloud/android/ui/preview/PreviewImageActivity.java b/src/com/owncloud/android/ui/preview/PreviewImageActivity.java index 47406bea..8dd02319 100644 --- a/src/com/owncloud/android/ui/preview/PreviewImageActivity.java +++ b/src/com/owncloud/android/ui/preview/PreviewImageActivity.java @@ -349,12 +349,9 @@ public class PreviewImageActivity extends SherlockFragmentActivity implements Fi 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); + } } } } @@ -384,15 +381,6 @@ public class PreviewImageActivity extends SherlockFragmentActivity implements Fi } - 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. * @@ -410,20 +398,19 @@ public class PreviewImageActivity extends SherlockFragmentActivity implements Fi 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"); } } diff --git a/src/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java b/src/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java index b6b28375..54ed582f 100644 --- a/src/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java +++ b/src/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java @@ -18,7 +18,9 @@ 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; @@ -30,6 +32,7 @@ import android.support.v4.app.FragmentManager; 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; @@ -52,8 +55,11 @@ public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter { private Account mAccount; private Set mObsoleteFragments; private Set mObsoletePositions; + private Set mDownloadErrors; private DataStorageManager mStorageManager; + private Map mCachedFragments; + /* private final FragmentManager mFragmentManager; private FragmentTransaction mCurTransaction = null; @@ -87,7 +93,9 @@ public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter { mImageFiles = mStorageManager.getDirectoryImages(parentFolder); mObsoleteFragments = new HashSet(); mObsoletePositions = new HashSet(); + mDownloadErrors = new HashSet(); //mFragmentManager = fragmentManager; + mCachedFragments = new HashMap(); } @@ -106,9 +114,16 @@ public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter { 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; } @@ -126,12 +141,34 @@ public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter { 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)) { @@ -142,21 +179,26 @@ public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter { } - /** - * 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. *