mirror of
https://github.com/peterosterlund2/droidfish.git
synced 2024-11-23 11:31:33 +01:00
Make changing languages work in Android 9
Based on information from: https://proandroiddev.com/change-language-programmatically-at-runtime-on-android-5e6bc15c758
This commit is contained in:
parent
ddf5209109
commit
4411b7f8b6
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue
Block a user