Browse Source

Hardening preview adapter for null files

Signed-off-by: tobiaskaminsky <tobias@kaminsky.me>
tobiaskaminsky 7 years ago
parent
commit
5d30d83e67

+ 12 - 10
src/main/java/com/owncloud/android/ui/preview/PreviewImageActivity.java

@@ -352,18 +352,20 @@ public class PreviewImageActivity extends FileActivity implements
         } else {
             OCFile currentFile = mPreviewImagePagerAdapter.getFileAt(position);
 
-            if (getSupportActionBar() != null && currentFile != null) {
-                getSupportActionBar().setTitle(currentFile.getFileName());
-            }
-            setDrawerIndicatorEnabled(false);
+            if (currentFile != null) {
+                if (getSupportActionBar() != null) {
+                    getSupportActionBar().setTitle(currentFile.getFileName());
+                }
+                setDrawerIndicatorEnabled(false);
 
-            if (currentFile.isEncrypted() && !currentFile.isDown() &&
-                    !mPreviewImagePagerAdapter.pendingErrorAt(position)) {
-                requestForDownload(currentFile);
-            }
+                if (currentFile.isEncrypted() && !currentFile.isDown() &&
+                        !mPreviewImagePagerAdapter.pendingErrorAt(position)) {
+                    requestForDownload(currentFile);
+                }
 
-            // Call to reset image zoom to initial state
-            ((PreviewImagePagerAdapter) mViewPager.getAdapter()).resetZoom();
+                // Call to reset image zoom to initial state
+                ((PreviewImagePagerAdapter) mViewPager.getAdapter()).resetZoom();
+            }
         }
 
     }

+ 34 - 0
src/main/java/com/owncloud/android/ui/preview/PreviewImageErrorFragment.java

@@ -0,0 +1,34 @@
+package com.owncloud.android.ui.preview;
+
+import android.os.Bundle;
+import android.support.annotation.Nullable;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+
+import com.owncloud.android.R;
+import com.owncloud.android.ui.fragment.FileFragment;
+
+import static com.owncloud.android.ui.activity.FileActivity.EXTRA_FILE;
+
+/**
+ * A fragment showing an error message
+ */
+
+public class PreviewImageErrorFragment extends FileFragment {
+    @Nullable
+    @Override
+    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
+        return inflater.inflate(R.layout.preview_image_error_fragment, container, false);
+    }
+
+    public static FileFragment newInstance() {
+        FileFragment fileFragment = new PreviewImageErrorFragment();
+        Bundle bundle = new Bundle();
+
+        bundle.putParcelable(EXTRA_FILE, null);
+        fileFragment.setArguments(bundle);
+
+        return fileFragment;
+    }
+}

+ 12 - 3
src/main/java/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java

@@ -140,10 +140,13 @@ public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter {
 
     
     public Fragment getItem(int i) {
-        OCFile file = mImageFiles.get(i);
+        OCFile file = getFileAt(i);
         Fragment fragment;
 
-        if (file.isDown()) {
+        if (file == null) {
+            fragment = PreviewImageErrorFragment.newInstance();
+
+        } else if (file.isDown()) {
             fragment = PreviewImageFragment.newInstance(file, mObsoletePositions.contains(i), false);
         } else {
             if (mDownloadErrors.contains(i)) {
@@ -174,7 +177,13 @@ public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter {
 
     @Override
     public CharSequence getPageTitle(int position) {
-        return mImageFiles.get(position).getFileName();
+        OCFile file = getFileAt(position);
+
+        if (file != null) {
+            return file.getFileName();
+        } else {
+            return "";
+        }
     }
 
     

+ 43 - 0
src/main/res/layout/preview_image_error_fragment.xml

@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?><!--
+  Nextcloud Android client application
+
+  Copyright (C) 2018 Tobias Kaminsky
+  Copyright (C) 2018 Nextcloud
+
+  This program is free software; you can redistribute it and/or
+  modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+  License as published by the Free Software Foundation; either
+  version 3 of the License, or any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+
+  You should have received a copy of the GNU Affero General Public
+  License along with this program.  If not, see <http://www.gnu.org/licenses/>.
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/top"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical"
+    android:animateLayoutChanges="true">
+
+    <ImageView
+        android:id="@+id/preview_error_image"
+        android:layout_width="72dp"
+        android:layout_height="72dp"
+        android:layout_gravity="center"
+        android:contentDescription="@string/file_icon"
+        android:src="@drawable/file_image" />
+
+    <TextView
+        android:id="@+id/preview_error_text"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center"
+        android:text="@string/preview_sorry"
+        android:textSize="26sp" />
+</LinearLayout>