[Android] Abort subs download if language code is not found

Geoffrey Métais git at videolan.org
Mon Apr 10 18:33:24 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Apr 10 18:31:13 2017 +0200| [a61ebc99d32c4f1ab35ec534203c632e62bd7058] | committer: Geoffrey Métais

Abort subs download if language code is not found

Also show an error message to encourage user to manually choose its
preferred languages

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

 vlc-android/res/values/strings.xml                    |  1 +
 .../org/videolan/vlc/util/SubtitlesDownloader.java    | 19 ++++++++++++-------
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index d31dc91..296a937 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -681,6 +681,7 @@
     <string name="list_languages_download_summary">Languages to download when fetching subtitles</string>
     <string name="list_languages_download_title">Subtitles download languages</string>
     <string name="service_unavailable">Service unavailable</string>
+    <string name="subs_dl_lang_fail">Failed to set subtitles language, try to set it in application settings</string>
     <string name="downloading_subtitles">Downloading subtitles</string>
     <string name="browse_folder">Browse folder</string>
     <string name="listen">Listen</string>
diff --git a/vlc-android/src/org/videolan/vlc/util/SubtitlesDownloader.java b/vlc-android/src/org/videolan/vlc/util/SubtitlesDownloader.java
index 3337615..74429dd 100644
--- a/vlc-android/src/org/videolan/vlc/util/SubtitlesDownloader.java
+++ b/vlc-android/src/org/videolan/vlc/util/SubtitlesDownloader.java
@@ -24,11 +24,13 @@
 
 package org.videolan.vlc.util;
 
+import android.annotation.TargetApi;
 import android.app.Activity;
 import android.app.ProgressDialog;
 import android.content.DialogInterface;
 import android.content.SharedPreferences;
 import android.net.Uri;
+import android.os.Build;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.Message;
@@ -42,13 +44,13 @@ import android.view.WindowManager;
 import android.widget.Toast;
 
 import org.videolan.libvlc.util.AndroidUtil;
+import org.videolan.medialibrary.media.MediaWrapper;
 import org.videolan.vlc.BuildConfig;
 import org.videolan.vlc.R;
 import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.gui.helpers.UiTools;
 import org.videolan.vlc.gui.video.VideoPlayerActivity;
 import org.videolan.vlc.media.MediaDatabase;
-import org.videolan.medialibrary.media.MediaWrapper;
 
 import java.io.File;
 import java.io.FileOutputStream;
@@ -99,18 +101,21 @@ public class SubtitlesDownloader {
         mContext = activity;
     }
 
+    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
     public void downloadSubs(final List<MediaWrapper> mediaList, Callback cb) {
         stop = false;
         mCallback = cb;
-        Set<String> languages;
+        Set<String> languages = null;
         try {
-            languages =  Collections.singleton(Locale.getDefault().getISO3Language().toLowerCase());
-        } catch (MissingResourceException e) {
-            languages = Collections.singleton(Locale.ENGLISH.getISO3Language().toLowerCase());
-        }
+            languages = Collections.singleton(Locale.getDefault().getISO3Language().toLowerCase());
+        } catch (MissingResourceException ignored) {}
         if (AndroidUtil.isHoneycombOrLater) {
             SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(VLCApplication.getAppContext());
-            languages =  pref.getStringSet("languages_download_list", languages);
+            languages = pref.getStringSet("languages_download_list", languages);
+        }
+        if (languages == null) { // In case getDefault().getISO3Language() fails
+            Toast.makeText(mContext, R.string.subs_dl_lang_fail, Toast.LENGTH_SHORT).show();
+            return;
         }
         final ArrayList<String> finalLanguages = new ArrayList<>(languages);
         VLCApplication.runBackground(new Runnable() {



More information about the Android mailing list