<div dir="ltr">LGTM, except that we have to check isCancelled() in doInBackground and onPostExecute</div><br><div class="gmail_quote"><div dir="ltr">On Wed, Jun 22, 2016 at 11:11 PM Thomas Guillem <<a href="mailto:thomas@gllm.fr">thomas@gllm.fr</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">---<br>
 .../vlc/gui/video/VideoPlayerActivity.java         | 84 +++++++++++++---------<br>
 1 file changed, 50 insertions(+), 34 deletions(-)<br>
<br>
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java<br>
index a1c8d1c..ec29967 100644<br>
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java<br>
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java<br>
@@ -39,6 +39,7 @@ import android.graphics.PixelFormat;<br>
 import android.media.AudioManager;<br>
 import android.media.MediaRouter;<br>
 import android.net.Uri;<br>
+import android.os.AsyncTask;<br>
 import android.os.Build;<br>
 import android.os.Bundle;<br>
 import android.os.Handler;<br>
@@ -125,6 +126,7 @@ import org.videolan.vlc.util.VLCInstance;<br>
 import java.io.ByteArrayInputStream;<br>
 import java.io.ByteArrayOutputStream;<br>
 import java.io.IOException;<br>
+import <a href="http://java.io" target="_blank">java.io</a>.InterruptedIOException;<br>
 import java.io.ObjectInputStream;<br>
 import java.io.ObjectOutputStream;<br>
 import <a href="http://java.io" target="_blank">java.io</a>.StreamCorruptedException;<br>
@@ -887,6 +889,10 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C<br>
                 editor.putLong(PreferencesActivity.VIDEO_RESUME_TIME, time);<br>
             }<br>
         }<br>
+<br>
+        if (mSubtitlesGetTask != null)<br>
+            mSubtitlesGetTask.cancel(true);<br>
+<br>
         // Save selected subtitles<br>
         String subtitleList_serialized = null;<br>
         if(mSubtitleSelectedFiles.size() > 0) {<br>
@@ -3015,43 +3021,53 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C<br>
             showOverlay(true);<br>
     }<br>
<br>
-    public void getSubtitles() {<br>
-        final String subtitleList_serialized = mSettings.getString(PreferencesActivity.VIDEO_SUBTITLE_FILES, null);<br>
-        VLCApplication.runBackground(new Runnable() {<br>
-            @Override<br>
-            public void run() {<br>
-                ArrayList<String> prefsList = new ArrayList<>();<br>
-                if(subtitleList_serialized != null) {<br>
-                    ByteArrayInputStream bis = new ByteArrayInputStream(subtitleList_serialized.getBytes());<br>
-                    try {<br>
-                        ObjectInputStream ois = new ObjectInputStream(bis);<br>
-                        prefsList = (ArrayList<String>)ois.readObject();<br>
-                    } catch(ClassNotFoundException e) {}<br>
-                    catch (StreamCorruptedException e) {}<br>
-                    catch (IOException e) {}<br>
-                }<br>
-                if (!TextUtils.equals(mUri.getScheme(), "fd"))<br>
-                    prefsList.addAll(MediaDatabase.getInstance().getSubtitles(mUri.getLastPathSegment()));<br>
-                for(String x : prefsList){<br>
-                    if(!mSubtitleSelectedFiles.contains(x))<br>
-                        mSubtitleSelectedFiles.add(x);<br>
-                }<br>
+    private SubtitlesGetTask mSubtitlesGetTask = null;<br>
+    private class SubtitlesGetTask extends AsyncTask<String, Void, ArrayList<String>> {<br>
+        @Override<br>
+        protected ArrayList<String> doInBackground(String... strings) {<br>
+            final String subtitleList_serialized = strings[0];<br>
+            ArrayList<String> prefsList = new ArrayList<>();<br>
+            if(subtitleList_serialized != null) {<br>
+                ByteArrayInputStream bis = new ByteArrayInputStream(subtitleList_serialized.getBytes());<br>
+                try {<br>
+                    ObjectInputStream ois = new ObjectInputStream(bis);<br>
+                    prefsList = (ArrayList<String>)ois.readObject();<br>
+                } catch (ClassNotFoundException | IOException ignored) {}<br>
+            }<br>
+            if (!TextUtils.equals(mUri.getScheme(), "fd"))<br>
+                prefsList.addAll(MediaDatabase.getInstance().getSubtitles(mUri.getLastPathSegment()));<br>
<br>
-                // Add any selected subtitle file from the file picker<br>
-                if(mSubtitleSelectedFiles.size() > 0) {<br>
-                    mHandler.post(new Runnable() {<br>
-                        @Override<br>
-                        public void run() {<br>
-                            if (mService != null)<br>
-                                for(String file : mSubtitleSelectedFiles) {<br>
-                                    Log.i(TAG, "Adding user-selected subtitle " + file);<br>
-                                    mService.addSubtitleTrack(file, true);<br>
-                                }<br>
-                        }<br>
-                    });<br>
+            return prefsList;<br>
+        }<br>
+<br>
+        @Override<br>
+        protected void onPostExecute(ArrayList<String> prefsList) {<br>
+            // Add any selected subtitle file from the file picker<br>
+            if (prefsList.size() > 0) {<br>
+                for (String file : prefsList) {<br>
+                    if (!mSubtitleSelectedFiles.contains(file)) {<br>
+                        mSubtitleSelectedFiles.add(file);<br>
+                        Log.i(TAG, "Adding user-selected subtitle " + file);<br>
+                        mService.addSubtitleTrack(file, true);<br>
+                    }<br>
                 }<br>
             }<br>
-        });<br>
+            mSubtitlesGetTask = null;<br>
+        }<br>
+<br>
+        @Override<br>
+        protected void onCancelled() {<br>
+            mSubtitlesGetTask = null;<br>
+        }<br>
+    }<br>
+<br>
+    public void getSubtitles() {<br>
+        if (mSubtitlesGetTask != null)<br>
+            return;<br>
+        final String subtitleList_serialized = mSettings.getString(PreferencesActivity.VIDEO_SUBTITLE_FILES, null);<br>
+<br>
+        mSubtitlesGetTask = new SubtitlesGetTask();<br>
+        mSubtitlesGetTask.execute(subtitleList_serialized);<br>
     }<br>
<br>
     @SuppressWarnings("deprecation")<br>
--<br>
2.8.1<br>
<br>
_______________________________________________<br>
Android mailing list<br>
<a href="mailto:Android@videolan.org" target="_blank">Android@videolan.org</a><br>
<a href="https://mailman.videolan.org/listinfo/android" rel="noreferrer" target="_blank">https://mailman.videolan.org/listinfo/android</a><br>
</blockquote></div>