[Android] [PATCH] Fix Locale problem on api > 26
Habib Kazemi
kazemihabib1996 at gmail.com
Fri Jul 13 22:52:09 CEST 2018
This patch fixes the locale change on api > 26
and also asks the user to restart the app now or not.
---
vlc-android/res/values/strings.xml | 4 ++
.../src/org/videolan/vlc/StartActivity.java | 3 ++
.../src/org/videolan/vlc/VLCApplication.java | 46 +++++---------------
.../src/org/videolan/vlc/gui/AboutFragment.java | 2 +-
.../vlc/gui/AudioPlayerContainerActivity.java | 4 +-
.../src/org/videolan/vlc/gui/helpers/UiTools.java | 49 ++++++++++++++++++++++
.../vlc/gui/preferences/PreferencesUi.java | 3 +-
.../vlc/gui/tv/preferences/PreferencesUi.java | 4 +-
8 files changed, 75 insertions(+), 40 deletions(-)
diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index d661e5968..586eeb26c 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -435,6 +435,10 @@
<string name="blurred_cover_background_summary">Blurred cover in audio player background</string>
<string name="network_caching_popup">This value must be between 0 and 6000 ms</string>
<string name="quit">Quit and restart application</string>
+ <string name="restart_title">Restart VLC</string>
+ <string name="restart_message">Restart needed for changes to take effect.\nRestart now?</string>
+ <string name="restart_message_OK">OK</string>
+ <string name="restart_message_Later">Later</string>
<string name="developer_prefs_category">Developer</string>
<string name="enable_verbose_mode">Verbose</string>
diff --git a/vlc-android/src/org/videolan/vlc/StartActivity.java b/vlc-android/src/org/videolan/vlc/StartActivity.java
index 401b21768..36dc6bab7 100644
--- a/vlc-android/src/org/videolan/vlc/StartActivity.java
+++ b/vlc-android/src/org/videolan/vlc/StartActivity.java
@@ -36,6 +36,7 @@ import android.text.TextUtils;
import org.videolan.libvlc.util.AndroidUtil;
import org.videolan.vlc.gui.MainActivity;
import org.videolan.vlc.gui.SearchActivity;
+import org.videolan.vlc.gui.helpers.UiTools;
import org.videolan.vlc.gui.helpers.hf.StoragePermissionsDelegate;
import org.videolan.vlc.gui.tv.MainTvActivity;
import org.videolan.vlc.gui.video.VideoPlayerActivity;
@@ -53,6 +54,8 @@ public class StartActivity extends FragmentActivity implements StoragePermission
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+ if (AndroidUtil.isNougatOrLater)
+ UiTools.setLocale(this);
final Intent intent = getIntent();
final boolean tv = showTvUi();
final String action = intent != null ? intent.getAction(): null;
diff --git a/vlc-android/src/org/videolan/vlc/VLCApplication.java b/vlc-android/src/org/videolan/vlc/VLCApplication.java
index f92391991..a281544fb 100644
--- a/vlc-android/src/org/videolan/vlc/VLCApplication.java
+++ b/vlc-android/src/org/videolan/vlc/VLCApplication.java
@@ -50,6 +50,8 @@ import java.lang.ref.WeakReference;
import java.util.Calendar;
import java.util.Locale;
+import static org.videolan.vlc.gui.helpers.UiTools.setLocale;
+
public class VLCApplication extends Application {
public final static String TAG = "VLC/VLCApplication";
@@ -66,6 +68,9 @@ public class VLCApplication extends Application {
private static int sDialogCounter = 0;
+ // Property to get the new locale only on restart to prevent change the locale partially on runtime
+ private static String locale = "";
+
public VLCApplication() {
super();
instance = this;
@@ -76,8 +81,10 @@ public class VLCApplication extends Application {
super.onCreate();
sSettings = PreferenceManager.getDefaultSharedPreferences(this);
sTV = AndroidDevices.isAndroidTv || (!AndroidDevices.isChromeBook && !AndroidDevices.hasTsp);
+ locale = sSettings.getString("set_locale", "");
- setLocale();
+ // Set the locale for API < 24 and set application resources and direction for API >=24
+ setLocale(instance);
WorkersKt.runBackground(new Runnable() {
@Override
@@ -96,7 +103,7 @@ public class VLCApplication extends Application {
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
- setLocale();
+ setLocale(instance);
}
/**
@@ -137,6 +144,8 @@ public class VLCApplication extends Application {
return sSettings;
}
+ public static String getLocale(){return locale;}
+
public static boolean showTvUi() {
return sTV || (sSettings != null && sSettings.getBoolean("tv_ui", false));
}
@@ -206,39 +215,6 @@ public class VLCApplication extends Application {
return Medialibrary.getInstance();
}
- public static void setLocale() {
- if (sSettings == null) PreferenceManager.getDefaultSharedPreferences(instance);
- // Are we using advanced debugging - locale?
- String p = sSettings.getString("set_locale", "");
- if (!p.equals("")) {
- Locale locale;
- // workaround due to region code
- if (p.equals("zh-TW")) {
- locale = Locale.TRADITIONAL_CHINESE;
- } else if(p.startsWith("zh")) {
- locale = Locale.CHINA;
- } else if(p.equals("pt-BR")) {
- locale = new Locale("pt", "BR");
- } else if(p.equals("bn-IN") || p.startsWith("bn")) {
- locale = new Locale("bn", "IN");
- } else {
- /**
- * Avoid a crash of
- * java.lang.AssertionError: couldn't initialize LocaleData for locale
- * if the user enters nonsensical region codes.
- */
- if(p.contains("-"))
- p = p.substring(0, p.indexOf('-'));
- locale = new Locale(p);
- }
- Locale.setDefault(locale);
- Configuration config = new Configuration();
- config.locale = locale;
- getAppResources().updateConfiguration(config,
- getAppResources().getDisplayMetrics());
- }
- }
-
/**
* Check if application is currently displayed
* @return true if an activity is displayed, false if app is in background.
diff --git a/vlc-android/src/org/videolan/vlc/gui/AboutFragment.java b/vlc-android/src/org/videolan/vlc/gui/AboutFragment.java
index b7480672f..7f63678bd 100644
--- a/vlc-android/src/org/videolan/vlc/gui/AboutFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/AboutFragment.java
@@ -63,7 +63,7 @@ public class AboutFragment extends Fragment {
//Fix android 7 Locale problem with webView
//https://stackoverflow.com/questions/40398528/android-webview-locale-changes-abruptly-on-android-n
if (AndroidUtil.isNougatOrLater)
- VLCApplication.setLocale();
+ UiTools.setLocale(getActivity());
final View aboutMain = v.findViewById(R.id.about_main);
final WebView webView = v.findViewById(R.id.webview);
diff --git a/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java b/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
index 87a4976de..0d6fe07a1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.java
@@ -89,7 +89,9 @@ public class AudioPlayerContainerActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) {
//Init Medialibrary if KO
if (savedInstanceState != null) {
- VLCApplication.setLocale();
+ if (AndroidUtil.isNougatOrLater)
+ UiTools.setLocale(this);
+
MediaParsingServiceKt.startMedialibrary(this, false, false, true);
}
super.onCreate(savedInstanceState);
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java b/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java
index 0e9623d12..5e149e4c1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java
@@ -30,6 +30,7 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
+import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.databinding.BindingAdapter;
import android.graphics.Bitmap;
@@ -85,6 +86,7 @@ import org.videolan.vlc.util.FileUtils;
import org.videolan.vlc.viewmodels.BaseModel;
import java.util.List;
+import java.util.Locale;
public class UiTools {
private static final String TAG = "VLC/UiTools";
@@ -457,4 +459,51 @@ public class UiTools {
winParams.rotationAnimation = AndroidUtil.isOOrLater ? WindowManager.LayoutParams.ROTATION_ANIMATION_SEAMLESS : WindowManager.LayoutParams.ROTATION_ANIMATION_JUMPCUT;
win.setAttributes(winParams);
}
+
+ public static void setLocale(Context context) {
+ // Are we using advanced debugging - locale?
+ String p = VLCApplication.getLocale();
+ if (!p.equals("")) {
+ Locale locale;
+ // workaround due to region code
+ if (p.equals("zh-TW")) {
+ locale = Locale.TRADITIONAL_CHINESE;
+ } else if(p.startsWith("zh")) {
+ locale = Locale.CHINA;
+ } else if(p.equals("pt-BR")) {
+ locale = new Locale("pt", "BR");
+ } else if(p.equals("bn-IN") || p.startsWith("bn")) {
+ locale = new Locale("bn", "IN");
+ } else {
+ /**
+ * Avoid a crash of
+ * java.lang.AssertionError: couldn't initialize LocaleData for locale
+ * if the user enters nonsensical region codes.
+ */
+ if(p.contains("-"))
+ p = p.substring(0, p.indexOf('-'));
+ locale = new Locale(p);
+ }
+ Locale.setDefault(locale);
+ Configuration config = new Configuration();
+ config.locale = locale;
+ context.getResources().updateConfiguration(config,
+ context.getResources().getDisplayMetrics());
+ }
+ }
+
+ public static void restartDialog(Context context) {
+ new AlertDialog.Builder(context)
+ .setTitle(context.getResources().getString(R.string.restart_title))
+ .setMessage(context.getResources().getString(R.string.restart_message))
+ .setPositiveButton(R.string.restart_message_OK, new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ android.os.Process.killProcess(android.os.Process.myPid());
+ }
+ })
+ .setNegativeButton(R.string.restart_message_Later, null)
+ .create()
+ .show();
+ }
}
diff --git a/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.java b/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.java
index 4a3b901b5..faa19b1a3 100644
--- a/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.java
+++ b/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.java
@@ -33,6 +33,7 @@ import org.videolan.libvlc.util.AndroidUtil;
import org.videolan.vlc.R;
import org.videolan.vlc.gui.helpers.UiTools;
+
public class PreferencesUi extends BasePreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
@Override
@@ -85,6 +86,6 @@ public class PreferencesUi extends BasePreferenceFragment implements SharedPrefe
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("set_locale"))
- UiTools.snacker(getView(), R.string.set_locale_popup);
+ UiTools.restartDialog(getActivity());
}
}
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/preferences/PreferencesUi.java b/vlc-android/src/org/videolan/vlc/gui/tv/preferences/PreferencesUi.java
index cbdda75b7..bd72a102e 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/preferences/PreferencesUi.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/preferences/PreferencesUi.java
@@ -27,9 +27,9 @@ import android.annotation.TargetApi;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
-import android.widget.Toast;
import org.videolan.vlc.R;
+import org.videolan.vlc.gui.helpers.UiTools;
import org.videolan.vlc.util.AndroidDevices;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@@ -75,7 +75,7 @@ public class PreferencesUi extends BasePreferenceFragment implements SharedPrefe
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
switch (key) {
case "set_locale":
- Toast.makeText(getActivity(), R.string.set_locale_popup, Toast.LENGTH_SHORT).show();
+ UiTools.restartDialog(getActivity());
break;
case "tv_ui":
((PreferencesActivity) getActivity()).setRestartApp();
--
2.11.0
More information about the Android
mailing list