From ad5b4327d1a57e74583ab2201657c640908b4327 Mon Sep 17 00:00:00 2001 From: CiufudeanDani Date: Tue, 19 Apr 2022 13:07:16 +0300 Subject: [PATCH] fix crash from language change --- .../safedispatch/SetupActivity.java | 152 ++++++------------ .../safedispatch/TabLayoutActivity.java | 2 - 2 files changed, 52 insertions(+), 102 deletions(-) diff --git a/safeDispatch/src/main/java/com/safemobile/safedispatch/SetupActivity.java b/safeDispatch/src/main/java/com/safemobile/safedispatch/SetupActivity.java index 6be79a5..1a9c92b 100644 --- a/safeDispatch/src/main/java/com/safemobile/safedispatch/SetupActivity.java +++ b/safeDispatch/src/main/java/com/safemobile/safedispatch/SetupActivity.java @@ -2,16 +2,13 @@ package com.safemobile.safedispatch; import java.util.Locale; -/** fix import */ import com.safemobile.adapters.LanguageSpinnerAdapter; import com.safemobile.lib.AppParams; import com.safemobile.lib.SM; import android.app.Activity; import android.app.AlertDialog; -import android.app.Dialog; import android.content.Context; -import android.content.DialogInterface; import android.content.SharedPreferences; import android.content.res.Configuration; import android.os.Bundle; @@ -73,52 +70,49 @@ public class SetupActivity extends Activity { setContentView(R.layout.tabsetup); - layoutSpinnerLanguage = (LinearLayout) findViewById(R.id.layoutSpinnerLanguage); + layoutSpinnerLanguage = findViewById(R.id.layoutSpinnerLanguage); layoutSpinnerLanguage.setOnClickListener(LanguageListener); - imageLanguage = (ImageView) findViewById(R.id.imageLanguage); - textViewSpinnerLanguage = (TextView) findViewById(R.id.textViewSpinnerLanguage); + imageLanguage = findViewById(R.id.imageLanguage); + textViewSpinnerLanguage = findViewById(R.id.textViewSpinnerLanguage); // change spinner icon and selected language according to tmpLanguage - if (AppParams.LANGUAGETMP.equals("en")){ - imageLanguage.setImageResource(R.drawable.en); - textViewSpinnerLanguage.setText(getString(R.string.en)); - } - else if (AppParams.LANGUAGETMP.equals("de")){ - imageLanguage.setImageResource(R.drawable.de); - textViewSpinnerLanguage.setText(getString(R.string.de)); - } - else if (AppParams.LANGUAGETMP.equals("tr")){ - imageLanguage.setImageResource(R.drawable.tr); - textViewSpinnerLanguage.setText(getString(R.string.tr)); - } - else if (AppParams.LANGUAGETMP.equals("ro")){ - imageLanguage.setImageResource(R.drawable.ro); - textViewSpinnerLanguage.setText(getString(R.string.ro)); - } - else if(AppParams.LANGUAGETMP.equals("es")){ - imageLanguage.setImageResource(R.drawable.es); - textViewSpinnerLanguage.setText(getString(R.string.es)); - } - else if(AppParams.LANGUAGETMP.equals("ru")){ - imageLanguage.setImageResource(R.drawable.ru); - textViewSpinnerLanguage.setText(getString(R.string.ru)); + switch (AppParams.LANGUAGETMP) { + case "en": + imageLanguage.setImageResource(R.drawable.en); + textViewSpinnerLanguage.setText(getString(R.string.en)); + break; + case "de": + imageLanguage.setImageResource(R.drawable.de); + textViewSpinnerLanguage.setText(getString(R.string.de)); + break; + case "tr": + imageLanguage.setImageResource(R.drawable.tr); + textViewSpinnerLanguage.setText(getString(R.string.tr)); + break; + case "ro": + imageLanguage.setImageResource(R.drawable.ro); + textViewSpinnerLanguage.setText(getString(R.string.ro)); + break; + case "es": + imageLanguage.setImageResource(R.drawable.es); + textViewSpinnerLanguage.setText(getString(R.string.es)); + break; + case "ru": + imageLanguage.setImageResource(R.drawable.ru); + textViewSpinnerLanguage.setText(getString(R.string.ru)); + break; } // get visual elements - appServerIP = (EditText) findViewById(R.id.appServerIP); - appServerPort = (EditText) findViewById(R.id.appServerPort); + appServerIP = findViewById(R.id.appServerIP); + appServerPort = findViewById(R.id.appServerPort); // hide path layout used only on Pad/Pod - layoutPath = (LinearLayout) findViewById(R.id.layoutPath); + layoutPath = findViewById(R.id.layoutPath); layoutPath.setVisibility(View.GONE); - btn_save = (Button) findViewById(R.id.btn_save); + btn_save = findViewById(R.id.btn_save); - btn_save.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - saveSettings(); - } - }); + btn_save.setOnClickListener(v -> saveSettings()); // set values if(!AppParams.IP.equals("n/a")) @@ -134,46 +128,31 @@ public class SetupActivity extends Activity { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(getString(R.string.exit)) .setCancelable(false) - .setNeutralButton(getString(R.string.logout), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - parentTab.whenBackPressed(AppParams.ActivityResult.logout); - } - }) - .setPositiveButton(getString(R.string.ext), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - parentTab.whenBackPressed(AppParams.ActivityResult.exit); - } - }) - .setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.cancel(); - } - }); + .setNeutralButton(getString(R.string.logout), (dialog, id) -> parentTab.whenBackPressed(AppParams.ActivityResult.logout)) + .setPositiveButton(getString(R.string.ext), (dialog, id) -> parentTab.whenBackPressed(AppParams.ActivityResult.exit)) + .setNegativeButton(getString(R.string.cancel), (dialog, id) -> dialog.cancel()); AlertDialog alert = builder.create(); alert.show(); } // listener when select language pressed - private OnClickListener LanguageListener = new OnClickListener() { + private final OnClickListener LanguageListener = new OnClickListener() { @Override public void onClick(View v) { final LanguageSpinnerAdapter adapter = new LanguageSpinnerAdapter(context, android.R.layout.simple_spinner_item, Languages, getLayoutInflater()); AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(getString(R.string.selLanguage)); - builder.setAdapter(adapter , new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - textViewSpinnerLanguage.setText(adapter.getItem(which)); - switch(which) - { - case 0: imageLanguage.setImageResource(R.drawable.en); AppParams.LANGUAGETMP = "en"; onCreate(savedInstanceState); parentTab.changeLanguage(); break; - case 1: imageLanguage.setImageResource(R.drawable.de); AppParams.LANGUAGETMP = "de"; onCreate(savedInstanceState); parentTab.changeLanguage(); break; - case 2: imageLanguage.setImageResource(R.drawable.tr); AppParams.LANGUAGETMP = "tr"; onCreate(savedInstanceState); parentTab.changeLanguage(); break; - case 3: imageLanguage.setImageResource(R.drawable.ro); AppParams.LANGUAGETMP = "ro"; onCreate(savedInstanceState); parentTab.changeLanguage(); break; - case 4: imageLanguage.setImageResource(R.drawable.ru); AppParams.LANGUAGETMP = "ru"; onCreate(savedInstanceState); parentTab.changeLanguage(); break; - case 5: imageLanguage.setImageResource(R.drawable.es); AppParams.LANGUAGETMP = "es"; onCreate(savedInstanceState); parentTab.changeLanguage(); break; - } + builder.setAdapter(adapter , (dialog, which) -> { + textViewSpinnerLanguage.setText(adapter.getItem(which)); + switch(which) + { + case 0: imageLanguage.setImageResource(R.drawable.en); AppParams.LANGUAGETMP = "en"; onCreate(savedInstanceState); parentTab.changeLanguage(); break; + case 1: imageLanguage.setImageResource(R.drawable.de); AppParams.LANGUAGETMP = "de"; onCreate(savedInstanceState); parentTab.changeLanguage(); break; + case 2: imageLanguage.setImageResource(R.drawable.tr); AppParams.LANGUAGETMP = "tr"; onCreate(savedInstanceState); parentTab.changeLanguage(); break; + case 3: imageLanguage.setImageResource(R.drawable.ro); AppParams.LANGUAGETMP = "ro"; onCreate(savedInstanceState); parentTab.changeLanguage(); break; + case 4: imageLanguage.setImageResource(R.drawable.ru); AppParams.LANGUAGETMP = "ru"; onCreate(savedInstanceState); parentTab.changeLanguage(); break; + case 5: imageLanguage.setImageResource(R.drawable.es); AppParams.LANGUAGETMP = "es"; onCreate(savedInstanceState); parentTab.changeLanguage(); break; } }); @@ -215,12 +194,12 @@ public class SetupActivity extends Activity { String oldIP = AppParams.IP; String oldPort = AppParams.PORT; - editor.putString("ip", appServerIP.getText().toString()); - editor.putString("port", appServerPort.getText().toString()); - editor.putString("language", AppParams.LANGUAGETMP); - Boolean result = editor.commit(); + editor.putString("ip", appServerIP.getText().toString()); + editor.putString("port", appServerPort.getText().toString()); + editor.putString("language", AppParams.LANGUAGETMP); + boolean result = editor.commit(); // saved completed - if(result) + if (result) Toast.makeText(context, "Settings saved successfully.", Toast.LENGTH_LONG).show(); else Toast.makeText(context, "Settings failed to complete!", Toast.LENGTH_LONG).show(); @@ -265,31 +244,4 @@ public class SetupActivity extends Activity { //showDialog(result); } - public void showDialog(Boolean result) - { - Dialog dialog = new Dialog(context); - if(result) - dialog.setTitle("Save Completed"); - else - dialog.setTitle("Save Failed"); - dialog.setContentView(R.layout.dialog); - dialog.setCancelable(true); - dialog.setCanceledOnTouchOutside(true); - - TextView text = (TextView) dialog.findViewById(R.id.text); - ImageView image = (ImageView) dialog.findViewById(R.id.image); - if(result) - { - image.setImageResource(R.drawable.error); - text.setText("Settings saved successfully."); - } - else - { - image.setImageResource(R.drawable.error); - text.setText("Settings failed to complete!"); - } - - dialog.show(); - } - } diff --git a/safeDispatch/src/main/java/com/safemobile/safedispatch/TabLayoutActivity.java b/safeDispatch/src/main/java/com/safemobile/safedispatch/TabLayoutActivity.java index 4b1d493..526fded 100644 --- a/safeDispatch/src/main/java/com/safemobile/safedispatch/TabLayoutActivity.java +++ b/safeDispatch/src/main/java/com/safemobile/safedispatch/TabLayoutActivity.java @@ -737,8 +737,6 @@ public class TabLayoutActivity extends AbstractSDParentActivity { getBaseContext().getResources().updateConfiguration(configuration, getBaseContext().getResources().getDisplayMetrics()); - if (getHistoryActivity() != null) - getHistoryActivity().onCreate(getHistoryActivity().savedInstanceState); // change UI for RadioActivity and MessageActivity if (getRadioActivity() != null) getRadioActivity().onCreate(getRadioActivity().savedInstanceState);