Protect the app against crashes due to fails in creation of thumbnails
authorDavid A. Velasco <dvelasco@solidgear.es>
Tue, 23 Sep 2014 11:43:30 +0000 (13:43 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Tue, 23 Sep 2014 11:43:30 +0000 (13:43 +0200)
src/com/owncloud/android/ui/adapter/FileListListAdapter.java

index 11874f7..93a7b30 100644 (file)
@@ -50,6 +50,7 @@ import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.ui.activity.ComponentsGetter;\r
 import com.owncloud.android.utils.BitmapUtils;\r
 import com.owncloud.android.utils.DisplayUtils;\r
 import com.owncloud.android.ui.activity.ComponentsGetter;\r
 import com.owncloud.android.utils.BitmapUtils;\r
 import com.owncloud.android.utils.DisplayUtils;\r
+import com.owncloud.android.utils.Log_OC;\r
 \r
 \r
 /**\r
 \r
 \r
 /**\r
@@ -57,11 +58,13 @@ import com.owncloud.android.utils.DisplayUtils;
  * instance.\r
  * \r
  * @author Bartek Przybylski\r
  * instance.\r
  * \r
  * @author Bartek Przybylski\r
- * @Author Tobias Kaminsky\r
- * \r
+ * @author Tobias Kaminsky\r
+ * @author David A. Velasco\r
  */\r
 public class FileListListAdapter extends BaseAdapter implements ListAdapter {\r
     private final static String PERMISSION_SHARED_WITH_ME = "S";\r
  */\r
 public class FileListListAdapter extends BaseAdapter implements ListAdapter {\r
     private final static String PERMISSION_SHARED_WITH_ME = "S";\r
+    \r
+    private static final String TAG = FileListListAdapter.class.getSimpleName();\r
 \r
     private Context mContext;\r
     private OCFile mFile = null;\r
 \r
     private Context mContext;\r
     private OCFile mFile = null;\r
@@ -139,62 +142,74 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
         // Decode image in background.\r
         @Override\r
         protected Bitmap doInBackground(OCFile... params) {\r
         // Decode image in background.\r
         @Override\r
         protected Bitmap doInBackground(OCFile... params) {\r
-            file = params[0];\r
-            final String imageKey = String.valueOf(file.getRemoteId());\r
-\r
-            // Check disk cache in background thread\r
-            Bitmap thumbnail = getBitmapFromDiskCache(imageKey);\r
-\r
-            // Not found in disk cache\r
-            if (thumbnail == null) { \r
-                // Converts dp to pixel\r
-                Resources r = mContext.getResources();\r
-                int px = (int) Math.round(TypedValue.applyDimension(\r
-                        TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics()\r
-                ));\r
-                \r
-                if (file.isDown()){\r
-                    Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(\r
-                            file.getStoragePath(), px, px);\r
+            Bitmap thumbnail = null;\r
+            \r
+            try {\r
+                file = params[0];\r
+                final String imageKey = String.valueOf(file.getRemoteId());\r
+    \r
+                // Check disk cache in background thread\r
+                thumbnail = getBitmapFromDiskCache(imageKey);\r
+    \r
+                // Not found in disk cache\r
+                if (thumbnail == null) { \r
+                    // Converts dp to pixel\r
+                    Resources r = mContext.getResources();\r
+                    int px = (int) Math.round(TypedValue.applyDimension(\r
+                            TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics()\r
+                    ));\r
                     \r
                     \r
-                    if (bitmap != null) {\r
-                        thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);\r
-\r
-                        // Add thumbnail to cache\r
-                        addBitmapToCache(imageKey, thumbnail);\r
+                    if (file.isDown()){\r
+                        Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(\r
+                                file.getStoragePath(), px, px);\r
+                        \r
+                        if (bitmap != null) {\r
+                            thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);\r
+    \r
+                            // Add thumbnail to cache\r
+                            addBitmapToCache(imageKey, thumbnail);\r
+                        }\r
+    \r
+                    } else {\r
+                        // Download thumbnail from server\r
+                        // Commented out as maybe changes to client library are needed\r
+    //                    DefaultHttpClient httpclient = new DefaultHttpClient();\r
+    //                    try {\r
+    //                        httpclient.getCredentialsProvider().setCredentials(\r
+    //                                new AuthScope(mClient.getBaseUri().toString().replace("https://", ""), 443), \r
+    //                                new UsernamePasswordCredentials(mClient.getCredentials().getUsername(), mClient.getCredentials().getAuthToken()));\r
+    //                        \r
+    //\r
+    //                        HttpGet httpget = new HttpGet(mClient.getBaseUri() + "/ocs/v1.php/thumbnail?x=50&y=50&path=" + URLEncoder.encode(file.getRemotePath(), "UTF-8"));\r
+    //                        HttpResponse response = httpclient.execute(httpget);\r
+    //                        HttpEntity entity = response.getEntity();\r
+    //                        \r
+    //                        if (entity != null) {\r
+    //                            byte[] bytes = EntityUtils.toByteArray(entity);\r
+    //                            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);\r
+    //                            thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);\r
+    //                            \r
+    //                            // Add thumbnail to cache\r
+    //                            if (thumbnail != null){\r
+    //                                addBitmapToCache(imageKey, thumbnail);\r
+    //                            }\r
+    //                        }\r
+    //                    } catch(Exception e){\r
+    //                        e.printStackTrace();\r
+    //                    }finally {\r
+    //                        httpclient.getConnectionManager().shutdown();\r
+    //                    }\r
                     }\r
                     }\r
-\r
-                } else {\r
-                    // Download thumbnail from server\r
-                    // Commented out as maybe changes to client library are needed\r
-//                    DefaultHttpClient httpclient = new DefaultHttpClient();\r
-//                    try {\r
-//                        httpclient.getCredentialsProvider().setCredentials(\r
-//                                new AuthScope(mClient.getBaseUri().toString().replace("https://", ""), 443), \r
-//                                new UsernamePasswordCredentials(mClient.getCredentials().getUsername(), mClient.getCredentials().getAuthToken()));\r
-//                        \r
-//\r
-//                        HttpGet httpget = new HttpGet(mClient.getBaseUri() + "/ocs/v1.php/thumbnail?x=50&y=50&path=" + URLEncoder.encode(file.getRemotePath(), "UTF-8"));\r
-//                        HttpResponse response = httpclient.execute(httpget);\r
-//                        HttpEntity entity = response.getEntity();\r
-//                        \r
-//                        if (entity != null) {\r
-//                            byte[] bytes = EntityUtils.toByteArray(entity);\r
-//                            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);\r
-//                            thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px);\r
-//                            \r
-//                            // Add thumbnail to cache\r
-//                            if (thumbnail != null){\r
-//                                addBitmapToCache(imageKey, thumbnail);\r
-//                            }\r
-//                        }\r
-//                    } catch(Exception e){\r
-//                        e.printStackTrace();\r
-//                    }finally {\r
-//                        httpclient.getConnectionManager().shutdown();\r
-//                    }\r
-                } \r
+                }\r
+                \r
+            } catch (Throwable t) {\r
+                // the app should never break due to a problem with thumbnails\r
+                Log_OC.e(TAG, "Generation of thumbnail for " + file + " failed", t);\r
+                if (t instanceof OutOfMemoryError) {\r
+                    System.gc();\r
+                }\r
             }\r
             }\r
+            \r
             return thumbnail;\r
         }\r
         \r
             return thumbnail;\r
         }\r
         \r