[Android] [PATCH] Add subtitle font preference

Edward Wang edward.c.wang at compdigitec.com
Mon Jan 20 22:42:38 CET 2014


Hello,

On 2014-01-20 13:45, Abir Viqar <nitsrite at gmail.com> wrote:
> ---
>   vlc-android/jni/libvlcjni.c                        |  7 +++
>   vlc-android/res/values/strings.xml                 |  1 +
>   vlc-android/res/xml/preferences.xml                |  3 ++
>   vlc-android/src/org/videolan/libvlc/LibVLC.java    |  9 ++++
>   vlc-android/src/org/videolan/vlc/Util.java         |  1 +
>   .../org/videolan/vlc/gui/PreferencesActivity.java  | 61 ++++++++++++++++++++++
>   6 files changed, 82 insertions(+)
>
> diff --git a/vlc-android/jni/libvlcjni.c b/vlc-android/jni/libvlcjni.c
> index cba3bc0..b8b5253 100644
> --- a/vlc-android/jni/libvlcjni.c
> +++ b/vlc-android/jni/libvlcjni.c
> @@ -240,6 +240,11 @@ void Java_org_videolan_libvlc_LibVLC_nativeInit(JNIEnv *env, jobject thiz)
>       const char *chromastr = (*env)->GetStringUTFChars(env, chroma, 0);
>       LOGD("Chroma set to \"%s\"", chromastr);
>   
> +    methodId = (*env)->GetMethodID(env, cls, "getSubtitleFont", "()Ljava/lang/String;");
> +    jstring subfont = (*env)->CallObjectMethod(env, thiz, methodId);
> +    const char *subfontstr = (*env)->GetStringUTFChars(env, subfont, 0);
> +    LOGD("Subtitle font set to \"%s\"", subfontstr);
Is this perhaps missing the corresponding ReleaseStringUTFChars?

> +
>       methodId = (*env)->GetMethodID(env, cls, "getSubtitlesEncoding", "()Ljava/lang/String;");
>       jstring subsencoding = (*env)->CallObjectMethod(env, thiz, methodId);
>       const char *subsencodingstr = (*env)->GetStringUTFChars(env, subsencoding, 0);
> @@ -262,6 +267,7 @@ void Java_org_videolan_libvlc_LibVLC_nativeInit(JNIEnv *env, jobject thiz)
>           "--preferred-resolution", "360",
>           "--avcodec-fast",
>           "--avcodec-threads=0",
> +        "--freetype-font", subfontstr,
It would be better to not add this option at all if it's not specified.

>           "--subsdec-encoding", subsencodingstr,
>           enable_time_stretch ? "--audio-time-stretch" : "--no-audio-time-stretch",
>           "--avcodec-skiploopfilter", deblockstr,
> @@ -277,6 +283,7 @@ void Java_org_videolan_libvlc_LibVLC_nativeInit(JNIEnv *env, jobject thiz)
>       setLong(env, thiz, "mLibVlcInstance", (jlong)(intptr_t) instance);
>   
>       (*env)->ReleaseStringUTFChars(env, chroma, chromastr);
> +    (*env)->ReleaseStringUTFChars(env, subfont, subfontstr);
>       (*env)->ReleaseStringUTFChars(env, subsencoding, subsencodingstr);
>   
>       if (!instance)
> diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
> index e7182e2..2e976d0 100644
> --- a/vlc-android/res/values/strings.xml
> +++ b/vlc-android/res/values/strings.xml
> @@ -210,6 +210,7 @@
>       <string name="clear_media_db">Clear media database</string>
>       <string name="media_db_cleared">Media database cleared!</string>
>       <string name="subtitles_text_encoding">Subtitle text encoding</string>
> +    <string name="subtitle_font">Subtitle font</string>
>   
>       <string name="debug_logs">Debug logs</string>
>       <string name="start_logging">Start logging</string>
> diff --git a/vlc-android/res/xml/preferences.xml b/vlc-android/res/xml/preferences.xml
> index 0c7b564..5479c30 100644
> --- a/vlc-android/res/xml/preferences.xml
> +++ b/vlc-android/res/xml/preferences.xml
> @@ -69,6 +69,9 @@
>       </PreferenceCategory>
>   
>       <PreferenceCategory android:title="@string/other_prefs_category" >
> +        <PreferenceScreen
> +            android:key="subtitle_font"
> +            android:title="@string/subtitle_font" />

Missing defaultValue.

>           <ListPreference
>               android:defaultValue=""
>               android:entries="@array/subtitles_encoding_list"
> diff --git a/vlc-android/src/org/videolan/libvlc/LibVLC.java b/vlc-android/src/org/videolan/libvlc/LibVLC.java
> index bcde3a5..e9bf33b 100644
> --- a/vlc-android/src/org/videolan/libvlc/LibVLC.java
> +++ b/vlc-android/src/org/videolan/libvlc/LibVLC.java
> @@ -59,6 +59,7 @@ public class LibVLC {
>   
>       /** Settings */
>       private boolean iomx = false;
> +    private String subtitleFont = "";
>       private String subtitlesEncoding = "";
>       private int aout = LibVlcUtil.isGingerbreadOrLater() ? AOUT_OPENSLES : AOUT_AUDIOTRACK_JAVA;
>       private int vout = VOUT_ANDROID_SURFACE;
> @@ -224,6 +225,14 @@ public class LibVLC {
>           this.iomx = iomx;
>       }
>   
> +    public String getSubtitleFont() {
> +        return subtitleFont;
> +    }
> +
> +    public void setSubtitleFont(String subtitleFont) {
> +        this.subtitleFont = subtitleFont;
> +    }
> +
>       public String getSubtitlesEncoding() {
>           return subtitlesEncoding;
>       }
> diff --git a/vlc-android/src/org/videolan/vlc/Util.java b/vlc-android/src/org/videolan/vlc/Util.java
> index db19fb8..b9c1a45 100644
> --- a/vlc-android/src/org/videolan/vlc/Util.java
> +++ b/vlc-android/src/org/videolan/vlc/Util.java
> @@ -116,6 +116,7 @@ public class Util {
>               return;
>   
>           instance.setIomx(pref.getBoolean("enable_iomx", false));
> +        instance.setSubtitleFont(pref.getString("subtitle_font", ""));
>           instance.setSubtitlesEncoding(pref.getString("subtitles_text_encoding", ""));
>           instance.setTimeStretching(pref.getBoolean("enable_time_stretching_audio", false));
>           instance.setFrameSkip(pref.getBoolean("enable_frame_skip", false));
> diff --git a/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java b/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
> index 20e6ae0..294a595 100644
> --- a/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
> +++ b/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
> @@ -20,6 +20,8 @@
>   
>   package org.videolan.vlc.gui;
>   
> +import java.io.File;
> +
>   import org.videolan.libvlc.LibVLC;
>   import org.videolan.vlc.AudioService;
>   import org.videolan.vlc.AudioServiceController;
> @@ -30,11 +32,14 @@ import org.videolan.vlc.Util;
>   import org.videolan.vlc.gui.audio.AudioUtil;
>   
>   import android.app.AlertDialog;
> +import android.content.ActivityNotFoundException;
>   import android.content.Context;
>   import android.content.DialogInterface;
>   import android.content.Intent;
> +import android.content.pm.PackageManager;
>   import android.content.SharedPreferences;
>   import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
> +import android.net.Uri;
>   import android.os.Bundle;
>   import android.preference.CheckBoxPreference;
>   import android.preference.EditTextPreference;
> @@ -44,6 +49,7 @@ import android.preference.Preference.OnPreferenceChangeListener;
>   import android.preference.Preference.OnPreferenceClickListener;
>   import android.preference.PreferenceActivity;
>   import android.preference.PreferenceManager;
> +import android.util.Log;
>   import android.widget.Toast;
>   
>   @SuppressWarnings("deprecation")
> @@ -55,6 +61,7 @@ public class PreferencesActivity extends PreferenceActivity implements OnSharedP
>       public final static String VIDEO_RESUME_TIME = "VideoResumeTime";
>       public final static String VIDEO_SUBTITLE_FILES = "VideoSubtitleFiles";
>       public final static int RESULT_RESCAN = RESULT_FIRST_USER + 1;
> +    public final static int PICK_FONT = 0;
>   
>       @Override
>       protected void onCreate(Bundle savedInstanceState) {
> @@ -111,6 +118,40 @@ public class PreferencesActivity extends PreferenceActivity implements OnSharedP
>                       }
>                   });
>   
> +        // Subtitle Font
> +        Preference subtitleFontPref = findPreference("subtitle_font");
> +        subtitleFontPref.setOnPreferenceClickListener(
> +                new OnPreferenceClickListener() {
> +                    @Override
> +                    public boolean onPreferenceClick(Preference preference) {
> +                        Intent intent = new Intent("org.openintents.action.PICK_FILE");
> +
> +                        File file = new File(android.os.Environment.getExternalStorageDirectory().getPath());
> +                        intent.setData(Uri.fromFile(file));
> +
> +                        if (PreferencesActivity.this.getPackageManager()
> +                                .queryIntentActivities(intent,
> +                                        PackageManager.MATCH_DEFAULT_ONLY).size() > 0) {
> +                            PreferencesActivity.this.startActivityForResult(intent, PICK_FONT);
> +                        } else {
> +                            // OI intent not found, trying anything
> +                            Intent intent2 = new Intent(Intent.ACTION_GET_CONTENT);
> +                            intent2.setType("*/*");
> +                            intent2.addCategory(Intent.CATEGORY_OPENABLE);
> +                            try {
> +                                PreferencesActivity.this.startActivityForResult(intent2, PICK_FONT);
> +                            } catch(ActivityNotFoundException e) {
> +                                Log.i(TAG, "No file picker found on system");
> +                                Toast.makeText(PreferencesActivity.this,
> +                                        R.string.no_file_picker_found,
> +                                        Toast.LENGTH_SHORT).show();
> +                            }
> +                        }
> +
> +                        return true;
How can the user reset this preference back to default (blank)?

> +                    }
> +                });
> +
>           // Clear search history
>           Preference clearHistoryPref = findPreference("clear_history");
>           clearHistoryPref.setOnPreferenceClickListener(
> @@ -228,6 +269,7 @@ public class PreferencesActivity extends PreferenceActivity implements OnSharedP
>       @Override
>       public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
>           if(key.equalsIgnoreCase("enable_iomx")
> +                || key.equalsIgnoreCase("subtitle_font")
>                   || key.equalsIgnoreCase("subtitles_text_encoding")
>                   || key.equalsIgnoreCase("aout")
>                   || key.equalsIgnoreCase("vout")
> @@ -254,6 +296,25 @@ public class PreferencesActivity extends PreferenceActivity implements OnSharedP
>           AudioServiceController.getInstance().unbindAudioService(this);
>       }
>   
> +    @Override
> +    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
> +        if(requestCode == PICK_FONT) {
> +            if(data == null) return;
> +
> +            if(data.getDataString() == null) {
> +                Log.d(TAG, "Subtitle font selection was cancelled");
> +            }
> +            if(data.getData() == null) return;
> +
> +            String uri = data.getData().getPath();
> +
> +            final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
> +            SharedPreferences.Editor editor = sharedPrefs.edit();
> +            editor.putString("subtitle_font", uri);
> +            editor.commit();
> +        }
> +    }
> +
>       private void restartService(Context context) {
>           Intent service = new Intent(context, AudioService.class);




More information about the Android mailing list