[Android] Prevent IndexOutOfBoundsException

Geoffrey Métais git at videolan.org
Fri Apr 10 18:39:17 CEST 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Apr 10 18:38:19 2015 +0200| [d6cb09b8774e90c04dea0d3de405dd15c391172e] | committer: Geoffrey Métais

Prevent IndexOutOfBoundsException

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=d6cb09b8774e90c04dea0d3de405dd15c391172e
---

 .../vlc/gui/audio/AudioBrowserFragment.java        |   21 ++++--
 .../videolan/vlc/gui/video/VideoGridFragment.java  |   71 ++++++++++----------
 2 files changed, 51 insertions(+), 41 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
index dbf8f26..3375a10 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
@@ -431,6 +431,8 @@ public class AudioBrowserFragment extends BrowserFragment implements SwipeRefres
                 adapter = mPlaylistAdapter;
             } else
                 return false;
+            if (adapter.getCount() <= groupPosition || adapter.getLocations(groupPosition) == null || adapter.getLocations(groupPosition).isEmpty())
+                return false;
             AlertDialog alertDialog = CommonDialogs.deleteMedia(
                     getActivity(),
                     adapter.getLocations(groupPosition).get(0),
@@ -455,29 +457,33 @@ public class AudioBrowserFragment extends BrowserFragment implements SwipeRefres
         }
 
         if (id == R.id.audio_list_browser_set_song) {
+            if (mSongsAdapter.getCount() <= groupPosition)
+                return false;
             AudioUtil.setRingtone(mSongsAdapter.getItem(groupPosition).mMediaList.get(0), getActivity());
             return true;
         }
 
         if (useAllItems) {
+            if (mSongsAdapter.getCount() <= groupPosition)
+                return false;
             medias = new ArrayList<String>();
             startPosition = mSongsAdapter.getListWithPosition(medias, groupPosition);
         }
         else {
             startPosition = 0;
-            switch (mode)
-            {
+            AudioBrowserListAdapter adapter = null;
+            switch (mode){
                 case MODE_SONG:
-                    medias = mSongsAdapter.getLocations(groupPosition);
+                    adapter = mSongsAdapter;
                     break;
                 case MODE_ARTIST:
-                    medias = mArtistsAdapter.getLocations(groupPosition);
+                    adapter = mArtistsAdapter;
                     break;
                 case MODE_ALBUM:
-                    medias = mAlbumsAdapter.getLocations(groupPosition, true);
+                    adapter = mAlbumsAdapter;
                     break;
                 case MODE_GENRE:
-                    medias = mGenresAdapter.getLocations(groupPosition);
+                    adapter = mGenresAdapter;
                     break;
                 case MODE_PLAYLIST: //For file playlist, we browse tracks with mediabrowser, and add them in callbacks onMediaAdded and onBrowseEnd
                     medias = mPlaylistAdapter.getLocations(groupPosition);
@@ -491,6 +497,9 @@ public class AudioBrowserFragment extends BrowserFragment implements SwipeRefres
                 default:
                     return true;
             }
+            if (groupPosition >= adapter.getCount())
+                return false;
+            medias = adapter.getLocations(groupPosition);
         }
 
         if (append)
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
index 28ca545..cd3d2a3 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
@@ -299,44 +299,45 @@ public class VideoGridFragment extends BrowserFragment implements ISortable, IVi
     }
 
     private boolean handleContextItemSelected(MenuItem menu, int position) {
+        if (position >= mVideoAdapter.getCount())
+            return false;
         MediaWrapper media = mVideoAdapter.getItem(position);
         if (media == null)
             return false;
-        switch (menu.getItemId())
-        {
-        case R.id.video_list_play_from_start:
-            playVideo(media, true);
-            return true;
-        case R.id.video_list_play_audio:
-            playAudio(media);
-            return true;
-        case R.id.video_list_info:
-            Activity activity = getActivity();
-            if (activity instanceof MainActivity)
-                ((MainActivity)activity).showSecondaryFragment("mediaInfo", media.getLocation());
-            else {
-                Intent i = new Intent(activity, SecondaryActivity.class);
-                i.putExtra("fragment", "mediaInfo");
-                i.putExtra("param", media.getLocation());
-                startActivity(i);
-            }
-            return true;
-        case R.id.video_list_delete:
-            AlertDialog alertDialog = CommonDialogs.deleteMedia(
-                    getActivity(),
-                    media.getLocation(),
-                    new VLCRunnable(media) {
-                        @Override
-                        public void run(Object o) {
-                            MediaWrapper media = (MediaWrapper) o;
-                            mMediaLibrary.getMediaItems().remove(media);
-                            mVideoAdapter.remove(media);
-                            if (mAudioController.getMediaLocations().contains(media.getLocation()))
-                                mAudioController.removeLocation(media.getLocation());
-                        }
-                    });
-            alertDialog.show();
-            return true;
+        switch (menu.getItemId()){
+            case R.id.video_list_play_from_start:
+                playVideo(media, true);
+                return true;
+            case R.id.video_list_play_audio:
+                playAudio(media);
+                return true;
+            case R.id.video_list_info:
+                Activity activity = getActivity();
+                if (activity instanceof MainActivity)
+                    ((MainActivity)activity).showSecondaryFragment("mediaInfo", media.getLocation());
+                else {
+                    Intent i = new Intent(activity, SecondaryActivity.class);
+                    i.putExtra("fragment", "mediaInfo");
+                    i.putExtra("param", media.getLocation());
+                    startActivity(i);
+                }
+                return true;
+            case R.id.video_list_delete:
+                AlertDialog alertDialog = CommonDialogs.deleteMedia(
+                        getActivity(),
+                        media.getLocation(),
+                        new VLCRunnable(media) {
+                            @Override
+                            public void run(Object o) {
+                                MediaWrapper media = (MediaWrapper) o;
+                                mMediaLibrary.getMediaItems().remove(media);
+                                mVideoAdapter.remove(media);
+                                if (mAudioController.getMediaLocations().contains(media.getLocation()))
+                                    mAudioController.removeLocation(media.getLocation());
+                            }
+                        });
+                alertDialog.show();
+                return true;
         }
         return false;
     }



More information about the Android mailing list