Browse Source

Improved message for failed downloads in gallery

David A. Velasco 12 years ago
parent
commit
db7eea712d

+ 20 - 1
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"
+	/>
+	
+	<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>
 

+ 44 - 20
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;
     };
     
 

+ 9 - 22
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");
                 }
                 
             }

+ 55 - 13
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<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;
@@ -87,7 +93,9 @@ public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter {
         mImageFiles = mStorageManager.getDirectoryImages(parentFolder); 
         mObsoleteFragments = new HashSet<Object>();
         mObsoletePositions = new HashSet<Integer>();
+        mDownloadErrors = new HashSet<Integer>();
         //mFragmentManager = fragmentManager;
+        mCachedFragments = new HashMap<Integer, FileFragment>();
     }
 
     
@@ -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.
      *