[Android] Add an optional callback for subs download

Geoffrey Métais git at videolan.org
Wed May 25 09:56:16 CEST 2016


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue May 24 17:37:59 2016 +0200| [bd1cf7c83ad19980e1b47b940f9b75115b2ebba9] | committer: Geoffrey Métais

Add an optional callback for subs download

> https://code.videolan.org/videolan/vlc-android/commit/bd1cf7c83ad19980e1b47b940f9b75115b2ebba9
---

 .../src/org/videolan/vlc/media/MediaUtils.java     | 12 ++++++-
 .../org/videolan/vlc/util/SubtitlesDownloader.java | 39 ++++++++++++++++------
 2 files changed, 39 insertions(+), 12 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/media/MediaUtils.java b/vlc-android/src/org/videolan/vlc/media/MediaUtils.java
index 43e19c7..f294c90 100644
--- a/vlc-android/src/org/videolan/vlc/media/MediaUtils.java
+++ b/vlc-android/src/org/videolan/vlc/media/MediaUtils.java
@@ -36,10 +36,20 @@ public class MediaUtils {
     }
 
     public static void getSubs(Activity activity, ArrayList<MediaWrapper> mediaList) {
+        getSubs(activity, mediaList, null);
+    }
+
+    public static void getSubs(Activity activity, ArrayList<MediaWrapper> mediaList, SubtitlesDownloader.Callback cb) {
         if (sSubtitlesDownloader == null)
             sSubtitlesDownloader = new SubtitlesDownloader();
         sSubtitlesDownloader.setActivity(activity);
-        sSubtitlesDownloader.downloadSubs(mediaList);
+        sSubtitlesDownloader.downloadSubs(mediaList, cb);
+    }
+
+    public static void getSubs(Activity activity, MediaWrapper media, SubtitlesDownloader.Callback cb) {
+        ArrayList<MediaWrapper> mediaList = new ArrayList<>();
+        mediaList.add(media);
+        getSubs(activity, mediaList, cb);
     }
 
     public static void getSubs(Activity activity, MediaWrapper media) {
diff --git a/vlc-android/src/org/videolan/vlc/util/SubtitlesDownloader.java b/vlc-android/src/org/videolan/vlc/util/SubtitlesDownloader.java
index 6dc56d5..9422eff 100644
--- a/vlc-android/src/org/videolan/vlc/util/SubtitlesDownloader.java
+++ b/vlc-android/src/org/videolan/vlc/util/SubtitlesDownloader.java
@@ -86,13 +86,19 @@ public class SubtitlesDownloader {
     private volatile boolean stop = false;
 
     private AlertDialog mSumUpDialog;
+    private Callback mCallback;
+
+    public interface Callback {
+        void onRequestEnded(boolean success);
+    }
 
     public void setActivity(Activity activity) {
         mContext = activity;
     }
 
-    public void downloadSubs(final List<MediaWrapper> mediaList) {
+    public void downloadSubs(final List<MediaWrapper> mediaList, Callback cb) {
         stop = false;
+        mCallback = cb;
         Set<String> languages =  Collections.singleton(Locale.getDefault().getISO3Language().toLowerCase());
         if (AndroidUtil.isHoneycombOrLater()) {
             SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mContext);
@@ -284,6 +290,13 @@ public class SubtitlesDownloader {
                     fails.put(fileName, langs);
             }
         }
+        if (mCallback != null)
+            mHandler.post(new Runnable() {
+                @Override
+                public void run() {
+                    mCallback.onRequestEnded(!success.isEmpty());
+                }
+            });
         if (!stop) {
             if (single){
                 stop = true;
@@ -414,18 +427,22 @@ public class SubtitlesDownloader {
     private String buildSumup(HashMap<String, ArrayList<String>> success, HashMap<String, ArrayList<String>> fails, boolean single) {
         StringBuilder textToDisplay = new StringBuilder();
         if (single){ // Text for the toast
-            for (Entry<String, ArrayList<String>> entry : success.entrySet()) {
-                textToDisplay.append(VLCApplication.getAppResources().getString(R.string.snack_subloader_sub_found));
-                if (entry.getValue().size() > 1)
-                    textToDisplay.append(" ").append(entry.getValue().toString()).append("\n");
+            if (!success.isEmpty()) {
+                for (Entry<String, ArrayList<String>> entry : success.entrySet()) {
+                    textToDisplay.append(VLCApplication.getAppResources().getString(R.string.snack_subloader_sub_found));
+                    if (entry.getValue().size() > 1)
+                        textToDisplay.append(" ").append(entry.getValue().toString()).append("\n");
+                }
             }
-            for (Entry<String, ArrayList<String>> entry : fails.entrySet()){
-                textToDisplay.append(VLCApplication.getAppResources().getString(R.string.snack_subloader_sub_not_found));
-                if (entry.getValue().size() > 1)
-                    textToDisplay.append(" ").append(entry.getValue().toString()).append("\n");
+            if (!fails.isEmpty()) {
+                for (Entry<String, ArrayList<String>> entry : fails.entrySet()){
+                    textToDisplay.append(VLCApplication.getAppResources().getString(R.string.snack_subloader_sub_not_found));
+                    if (entry.getValue().size() > 1)
+                        textToDisplay.append(" ").append(entry.getValue().toString()).append("\n");
+                }
             }
         } else { // Text for the dialog box
-            if (success.size()>0){
+            if (!success.isEmpty()){
                 textToDisplay.append(VLCApplication.getAppResources().getString(R.string.dialog_subloader_success)).append("\n");
 
                 for (Entry<String, ArrayList<String>> entry : success.entrySet()){
@@ -438,7 +455,7 @@ public class SubtitlesDownloader {
                     textToDisplay.append("\n\n");
             }
 
-            if (fails.size()>0){
+            if (!fails.isEmpty()){
                 textToDisplay.append(VLCApplication.getAppResources().getString(R.string.dialog_subloader_fails)).append("\n");
 
                 for (Entry<String, ArrayList<String>> entry : fails.entrySet()){



More information about the Android mailing list