diff --git a/DroidFishApp/src/main/java/com/kalab/chess/enginesupport/ChessEngine.java b/DroidFishApp/src/main/java/com/kalab/chess/enginesupport/ChessEngine.java index 4b335bc..bf752c3 100644 --- a/DroidFishApp/src/main/java/com/kalab/chess/enginesupport/ChessEngine.java +++ b/DroidFishApp/src/main/java/com/kalab/chess/enginesupport/ChessEngine.java @@ -67,6 +67,7 @@ public class ChessEngine { } private void copyFile(InputStream istream, String targetFilePath) throws IOException { + new File(targetFilePath).delete(); FileOutputStream fout = new FileOutputStream(targetFilePath); byte[] b = new byte[1024]; int numBytes = 0; diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/DroidFish.java b/DroidFishApp/src/main/java/org/petero/droidfish/DroidFish.java index ee88fce..ead55cf 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/DroidFish.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/DroidFish.java @@ -562,7 +562,7 @@ public class DroidFish extends Activity egtbForceReload = true; if (speech == null) speech = new Speech(); - readPrefs(); + readPrefs(false); TimeControlData tcData = new TimeControlData(); tcData.setTimeControl(timeControl, movesPerSession, timeIncrement); ctrl.newGame(gameMode, tcData); @@ -611,6 +611,11 @@ public class DroidFish extends Activity startTourGuide(); } + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(DroidFishApp.setLanguage(newBase, false)); + } + private void startTourGuide(){ if (!guideShowOnStart) return; @@ -852,7 +857,7 @@ public class DroidFish extends Activity ChessBoardPlay oldCB = cb; String statusStr = status.getText().toString(); initUI(); - readPrefs(); + readPrefs(true); cb.cursorX = oldCB.cursorX; cb.cursorY = oldCB.cursorY; cb.cursorVisible = oldCB.cursorVisible; @@ -892,7 +897,7 @@ public class DroidFish extends Activity if (leftHanded != leftHandedView()) reInitUI(); else - readPrefs(); + readPrefs(true); maybeAutoModeOff(gameMode); ctrl.setGameMode(gameMode); } @@ -1219,7 +1224,7 @@ public class DroidFish extends Activity return Integer.parseInt(tmp); } - private void readPrefs() { + private void readPrefs(boolean restartIfLangChange) { int modeNr = getIntSetting("gameMode", 1); gameMode = new GameMode(modeNr); String oldPlayerName = playerName; @@ -1268,8 +1273,7 @@ public class DroidFish extends Activity boolean useWakeLock = settings.getBoolean("wakeLock", false); setWakeLock(useWakeLock); - String lang = settings.getString("language", "default"); - setLanguage(lang); + DroidFishApp.setLanguage(this, restartIfLangChange); int fontSize = getIntSetting("fontSize", 12); int statusFontSize = fontSize; Configuration config = getResources().getConfiguration(); @@ -1359,28 +1363,6 @@ public class DroidFish extends Activity Util.overrideViewAttribs(findViewById(R.id.main)); } - private void setLanguage(String lang) { - if (Build.VERSION.SDK_INT >= 28) - return; // Unknown how to make this work for API level 28 - Locale newLocale; - if ("default".equals(lang)) { - newLocale = Resources.getSystem().getConfiguration().locale; - } else if (lang.contains("_")) { - String[] parts = lang.split("_"); - newLocale = new Locale(parts[0], parts[1]); - } else { - newLocale = new Locale(lang); - } - if (!newLocale.getLanguage().equals(Locale.getDefault().getLanguage())) { - Resources res = getResources(); - Configuration config = res.getConfiguration(); - config.locale = newLocale; - res.updateConfiguration(config, res.getDisplayMetrics()); - Locale.setDefault(newLocale); - recreate(); - } - } - /** * Change the Pieces into figurine or regular (i.e. letters) display */ diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/DroidFishApp.java b/DroidFishApp/src/main/java/org/petero/droidfish/DroidFishApp.java index 2df2cd7..490a3e7 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/DroidFishApp.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/DroidFishApp.java @@ -20,8 +20,16 @@ package org.petero.droidfish; import android.app.Application; import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; +import android.preference.PreferenceManager; import android.widget.Toast; +import java.util.Locale; + public class DroidFishApp extends Application { private static Context appContext; private static Toast toast; @@ -37,6 +45,51 @@ public class DroidFishApp extends Application { return appContext; } + @Override + protected void attachBaseContext(Context base) { + super.attachBaseContext(setLanguage(base, false)); + } + + @Override + public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + setLanguage(appContext, false); + } + + public static Context setLanguage(Context context, boolean restartIfLangChange) { + Context ret = context; + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); + String lang = settings.getString("language", "default"); + Locale newLocale; + if ("default".equals(lang)) { + newLocale = Resources.getSystem().getConfiguration().locale; + } else if (lang.contains("_")) { + String[] parts = lang.split("_"); + newLocale = new Locale(parts[0], parts[1]); + } else { + newLocale = new Locale(lang); + } + String currLang = context.getResources().getConfiguration().locale.getLanguage(); + if (!newLocale.getLanguage().equals(currLang)) { + Locale.setDefault(newLocale); + Resources res = context.getResources(); + Configuration config = new Configuration(res.getConfiguration()); + if (Build.VERSION.SDK_INT >= 17) { + config.setLocale(newLocale); + ret = context.createConfigurationContext(config); + } else { + config.locale = newLocale; + res.updateConfiguration(config, res.getDisplayMetrics()); + } + if (restartIfLangChange) { + Intent i = new Intent(context, DroidFish.class); + context.startActivity(i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | + Intent.FLAG_ACTIVITY_NEW_TASK)); + } + } + return ret; + } + /** Show a toast after canceling current toast. */ public static void toast(int resId, int duration) { if (toast != null) { diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/activities/CPUWarning.java b/DroidFishApp/src/main/java/org/petero/droidfish/activities/CPUWarning.java index 006179e..22a7ee5 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/activities/CPUWarning.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/activities/CPUWarning.java @@ -18,12 +18,14 @@ package org.petero.droidfish.activities; +import org.petero.droidfish.DroidFishApp; import org.petero.droidfish.R; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; +import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; @@ -51,4 +53,9 @@ public class CPUWarning extends Activity { DialogFragment df = new Fragment(); df.show(getFragmentManager(), ""); } + + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(DroidFishApp.setLanguage(newBase, false)); + } } diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditBoard.java b/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditBoard.java index 2e4b3ab..a79545f 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditBoard.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditBoard.java @@ -117,6 +117,11 @@ public class EditBoard extends Activity { checkValidAndUpdateMaterialDiff(); } + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(DroidFishApp.setLanguage(newBase, false)); + } + @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditOptions.java b/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditOptions.java index 09e6eeb..2e76ada 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditOptions.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditOptions.java @@ -19,6 +19,7 @@ package org.petero.droidfish.activities; import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; @@ -34,6 +35,7 @@ import android.widget.ArrayAdapter; import androidx.databinding.DataBindingUtil; +import org.petero.droidfish.DroidFishApp; import org.petero.droidfish.R; import org.petero.droidfish.Util; import org.petero.droidfish.databinding.EditoptionsBinding; @@ -72,6 +74,11 @@ public class EditOptions extends Activity { } } + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(DroidFishApp.setLanguage(newBase, false)); + } + @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditPGN.java b/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditPGN.java index 075c28d..cfaacf0 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditPGN.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/activities/EditPGN.java @@ -22,6 +22,7 @@ import android.app.AlertDialog; import android.app.Dialog; import android.app.ListActivity; import android.app.ProgressDialog; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; @@ -182,6 +183,11 @@ public abstract class EditPGN extends ListActivity { } } + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(DroidFishApp.setLanguage(newBase, false)); + } + @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/activities/LoadFEN.java b/DroidFishApp/src/main/java/org/petero/droidfish/activities/LoadFEN.java index 92e529a..aa1a99d 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/activities/LoadFEN.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/activities/LoadFEN.java @@ -24,6 +24,7 @@ import android.app.DialogFragment; import android.app.Fragment; import android.app.ListActivity; import android.app.ProgressDialog; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; @@ -154,6 +155,11 @@ public class LoadFEN extends ListActivity { } } + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(DroidFishApp.setLanguage(newBase, false)); + } + @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/activities/LoadScid.java b/DroidFishApp/src/main/java/org/petero/droidfish/activities/LoadScid.java index 878fb6a..c20b621 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/activities/LoadScid.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/activities/LoadScid.java @@ -36,6 +36,7 @@ import android.app.Fragment; import android.app.ListActivity; import android.app.LoaderManager; import android.app.ProgressDialog; +import android.content.Context; import android.content.CursorLoader; import android.content.DialogInterface; import android.content.Intent; @@ -184,6 +185,11 @@ public class LoadScid extends ListActivity { } } + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(DroidFishApp.setLanguage(newBase, false)); + } + @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/activities/Preferences.java b/DroidFishApp/src/main/java/org/petero/droidfish/activities/Preferences.java index 6f07145..89984d3 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/activities/Preferences.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/activities/Preferences.java @@ -18,9 +18,11 @@ package org.petero.droidfish.activities; +import org.petero.droidfish.DroidFishApp; import org.petero.droidfish.R; import org.petero.droidfish.Util; +import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; @@ -85,6 +87,11 @@ public class Preferences extends PreferenceActivity { Util.setFullScreenMode(this, settings); } + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(DroidFishApp.setLanguage(newBase, false)); + } + @Override protected void onPause() { super.onPause();