Added language support on Android Viewer

Change-Id: I05a189dcee9cf4415ce59771da03c08cf0fef6c4
Signed-off-by: Mert Tumer <[email protected]>
Reviewed-on: https://gerrit.libreoffice.org/60727
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <[email protected]>
diff --git a/android/source/res/values/arrays.xml b/android/source/res/values/arrays.xml
index f232676..f331e67 100644
--- a/android/source/res/values/arrays.xml
+++ b/android/source/res/values/arrays.xml
@@ -12,6 +12,9 @@
        <item >1</item>
        <item >2</item>
    </string-array>
    <string-array name="SupportedLanguagesValues">
        <item >en</item>
    </string-array>
    <string-array name="SortModeStringValues">
        <item >0</item>
        <item >1</item>
@@ -36,6 +39,9 @@
        <item>@string/filter_presentations</item>
        <item>@string/filter_drawings</item>
    </string-array>
    <string-array name="SupportedLanguages">
        <item>English</item>
    </string-array>
    <string-array name="SortModeNames">
        <item >@string/sort_az</item>
        <item >@string/sort_za</item>
diff --git a/android/source/res/xml/libreoffice_preferences.xml b/android/source/res/xml/libreoffice_preferences.xml
index 3bc04d7..86ad3af 100644
--- a/android/source/res/xml/libreoffice_preferences.xml
+++ b/android/source/res/xml/libreoffice_preferences.xml
@@ -22,6 +22,15 @@
            android:title="@string/pref_file_explorer_title"
            android:key="EXPLORER_VIEW_TYPE"
            android:summary="@string/pref_viewmode_summary" />

        <ListPreference
            android:title="Display Language"
            android:summary="Set the default display language"
            android:entries="@array/SupportedLanguages"
            android:entryValues="@array/SupportedLanguagesValues"
            android:key="DISPLAY_LANGUAGE"
            />

        <CheckBoxPreference
            android:title="@string/pref_show_hidden_files"
            android:key="ENABLE_SHOW_HIDDEN_FILES"
diff --git a/android/source/src/java/org/libreoffice/LibreOfficeApplication.java b/android/source/src/java/org/libreoffice/LibreOfficeApplication.java
index 67655c7..07c1466 100644
--- a/android/source/src/java/org/libreoffice/LibreOfficeApplication.java
+++ b/android/source/src/java/org/libreoffice/LibreOfficeApplication.java
@@ -11,6 +11,7 @@
package org.libreoffice;

import android.app.Application;
import android.content.Context;
import android.os.Handler;

public class LibreOfficeApplication extends Application {
@@ -24,4 +25,9 @@ public class LibreOfficeApplication extends Application {
    public static Handler getMainHandler() {
        return mainHandler;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(LocaleHelper.onAttach(base, "en"));
    }
}
diff --git a/android/source/src/java/org/libreoffice/LocaleHelper.java b/android/source/src/java/org/libreoffice/LocaleHelper.java
new file mode 100644
index 0000000..26a31e4
--- /dev/null
+++ b/android/source/src/java/org/libreoffice/LocaleHelper.java
@@ -0,0 +1,56 @@
package org.libreoffice;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;

import java.util.Locale;

public class LocaleHelper {

    private static final String SELECTED_LANG = "org.libreoffice.selected.lang";

    public static Context onAttach(Context context){
        String lang = getPersistedData(context, Locale.getDefault().getLanguage());
        return setLocale(context, lang);
    }

    public static Context onAttach(Context context, String defLang){
        String lang = getPersistedData(context, defLang);
        return setLocale(context, lang);
    }

    public static Context setLocale(Context context, String lang) {
        persist(context, lang);
        return updateResources(context, lang);
    }

    @SuppressWarnings("deprecation")
    private static Context updateResources(Context context, String lang) {
        Locale locale = new Locale(lang);
        Locale.setDefault(locale);

        Resources res = context.getResources();
        Configuration cfg = res.getConfiguration();
        cfg.locale = locale;
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
            cfg.setLayoutDirection(locale);

        res.updateConfiguration(cfg, res.getDisplayMetrics());
        return context;
    }

    private static void persist(Context context, String lang) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        preferences.edit().putString(SELECTED_LANG, lang);
        preferences.edit().apply();
    }

    private static String getPersistedData(Context context, String lang) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(SELECTED_LANG, lang);
    }
}
diff --git a/android/source/src/java/org/libreoffice/SettingsActivity.java b/android/source/src/java/org/libreoffice/SettingsActivity.java
index 4ef7277..de5f388 100644
--- a/android/source/src/java/org/libreoffice/SettingsActivity.java
+++ b/android/source/src/java/org/libreoffice/SettingsActivity.java
@@ -48,6 +48,9 @@ public class SettingsActivity extends Activity {
        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            SettingsListenerModel.getInstance().changePreferenceState(sharedPreferences, key);
            if(key.equals("DISPLAY_LANGUAGE")){
                getActivity().recreate();
            }
        }
    }
}
diff --git a/android/source/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java b/android/source/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java
index fd1e2a2..d6501f0 100644
--- a/android/source/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java
+++ b/android/source/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java
@@ -67,6 +67,7 @@ import android.widget.Toast;
import org.libreoffice.AboutDialogFragment;
import org.libreoffice.LOKitShell;
import org.libreoffice.LibreOfficeMainActivity;
import org.libreoffice.LocaleHelper;
import org.libreoffice.R;
import org.libreoffice.SettingsActivity;
import org.libreoffice.SettingsListenerModel;
@@ -95,6 +96,8 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings
    private int viewMode;
    private int sortMode;
    private boolean showHiddenFiles;
    private String displayLanguage;

    // dynamic permissions IDs
    private static final int PERMISSION_READ_EXTERNAL_STORAGE = 0;

@@ -115,6 +118,7 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings
    public static final String SORT_MODE_KEY = "SORT_MODE";
    private static final String RECENT_DOCUMENTS_KEY = "RECENT_DOCUMENTS";
    private static final String ENABLE_SHOW_HIDDEN_FILES_KEY = "ENABLE_SHOW_HIDDEN_FILES";
    private static final String DISPLAY_LANGUAGE = "DISPLAY_LANGUAGE";

    public static final String NEW_FILE_PATH_KEY = "NEW_FILE_PATH_KEY";
    public static final String NEW_DOC_TYPE_KEY = "NEW_DOC_TYPE_KEY";
@@ -148,7 +152,6 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings
    private LinearLayout impressLayout;
    private LinearLayout calcLayout;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
@@ -173,6 +176,11 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings
        fabCloseAnimation = AnimationUtils.loadAnimation(this, R.anim.fab_close);
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(LocaleHelper.onAttach(newBase,"en"));
    }

    public void createUI() {

        setContentView(R.layout.activity_document_browser);
@@ -834,6 +842,7 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings
        viewMode = Integer.valueOf(defaultPrefs.getString(EXPLORER_VIEW_TYPE_KEY, ""+ GRID_VIEW));
        filterMode = Integer.valueOf(defaultPrefs.getString(FILTER_MODE_KEY , "-1"));
        showHiddenFiles = defaultPrefs.getBoolean(ENABLE_SHOW_HIDDEN_FILES_KEY, false);
        displayLanguage = defaultPrefs.getString(DISPLAY_LANGUAGE, "en");

        Intent i = this.getIntent();
        if (i.hasExtra(CURRENT_DIRECTORY_KEY)) {
@@ -855,6 +864,8 @@ public class LibreOfficeUIActivity extends AppCompatActivity implements Settings
            viewMode = i.getIntExtra( EXPLORER_VIEW_TYPE_KEY, GRID_VIEW);
            Log.d(LOGTAG, EXPLORER_VIEW_TYPE_KEY);
        }

        LocaleHelper.setLocale(this, displayLanguage);
    }

    @Override