Gallery2: Fix potential crash

Author: Michael W <baddaemon87@gmail.com>
Date:   Wed Jul 12 14:16:42 2017 +0200

    Gallery2: Fix potential crash

    * A race between deleting and displaying videos seems to report a higher
      count of videos available than actually accessible.

    BUGBASH-489

    Change-Id: Iac24002feb8701cc4182c3834f663be017b625d5

Author: KhabarlakKonstantin <habarlack@gmail.com>
Date:   Tue Oct 3 11:22:40 2017 +0300

    Fix crash after deleting a video on Albums tab

    getMediaItem can return empty list if no more media items available,
    break folder processing then
    Number of items returned by getMediaItem can be inconsistent with
    getMediaItemCount

    Change-Id: I7f2abce5d3832a1b2c1fe068b629a3474ed742e2

Change-Id: Ibcaeff698da118f484a6f38528b4a2111a009fdd
diff --git a/src/com/android/gallery3d/app/AlbumPage.java b/src/com/android/gallery3d/app/AlbumPage.java
index 7f947d8..c3ef4c3 100644
--- a/src/com/android/gallery3d/app/AlbumPage.java
+++ b/src/com/android/gallery3d/app/AlbumPage.java
@@ -652,9 +652,17 @@
         if (mMediaSet == null)
             return false;
         int count = mMediaSet.getMediaItemCount();
+        ArrayList<MediaItem> list;
         MediaItem item;
         for (int i = 0; i < count; i++) {
-            item = mMediaSet.getMediaItem(i, 1).get(0);
+            list = mMediaSet.getMediaItem(i, 1);
+            if (list == null) {
+                continue;
+            }
+            if (list.isEmpty()) {
+                break;
+            }
+            item = list.get(0);
             if (item == null) {
                 continue;
             }