WIP: change of language works

This commit is contained in:
Roumen Georgiev 2018-04-30 16:41:40 +03:00
parent a0463742fa
commit 3f0c2958c7
3 changed files with 36 additions and 571 deletions

View file

@ -1,44 +1,11 @@
package info.nightscout.androidaps.startupwizard; package info.nightscout.androidaps.startupwizard;
//import org.mozilla.javascript.Context;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import android.content.BroadcastReceiver; import info.nightscout.androidaps.MainApp;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Display;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.plugins.NSClientInternal.NSClientPlugin; import info.nightscout.androidaps.plugins.NSClientInternal.NSClientPlugin;
import info.nightscout.utils.LocaleHelper; import info.nightscout.utils.LocaleHelper;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;
@ -52,532 +19,8 @@ public class SWDefinition {
swDefinition = new SWDefinition(); swDefinition = new SWDefinition();
return swDefinition; return swDefinition;
} }
android.content.Context context = new Context() { android.content.Context context = MainApp.instance().getApplicationContext();
@Override
public AssetManager getAssets() {
return null;
}
@Override
public Resources getResources() {
return null;
}
@Override
public PackageManager getPackageManager() {
return null;
}
@Override
public ContentResolver getContentResolver() {
return null;
}
@Override
public Looper getMainLooper() {
return null;
}
@Override
public Context getApplicationContext() {
return null;
}
@Override
public void setTheme(int resid) {
}
@Override
public Resources.Theme getTheme() {
return null;
}
@Override
public ClassLoader getClassLoader() {
return null;
}
@Override
public String getPackageName() {
return null;
}
@Override
public ApplicationInfo getApplicationInfo() {
return null;
}
@Override
public String getPackageResourcePath() {
return null;
}
@Override
public String getPackageCodePath() {
return null;
}
@Override
public SharedPreferences getSharedPreferences(String name, int mode) {
return null;
}
@Override
public boolean moveSharedPreferencesFrom(Context sourceContext, String name) {
return false;
}
@Override
public boolean deleteSharedPreferences(String name) {
return false;
}
@Override
public FileInputStream openFileInput(String name) throws FileNotFoundException {
return null;
}
@Override
public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException {
return null;
}
@Override
public boolean deleteFile(String name) {
return false;
}
@Override
public File getFileStreamPath(String name) {
return null;
}
@Override
public File getDataDir() {
return null;
}
@Override
public File getFilesDir() {
return null;
}
@Override
public File getNoBackupFilesDir() {
return null;
}
@Nullable
@Override
public File getExternalFilesDir(@Nullable String type) {
return null;
}
@Override
public File[] getExternalFilesDirs(String type) {
return new File[0];
}
@Override
public File getObbDir() {
return null;
}
@Override
public File[] getObbDirs() {
return new File[0];
}
@Override
public File getCacheDir() {
return null;
}
@Override
public File getCodeCacheDir() {
return null;
}
@Nullable
@Override
public File getExternalCacheDir() {
return null;
}
@Override
public File[] getExternalCacheDirs() {
return new File[0];
}
@Override
public File[] getExternalMediaDirs() {
return new File[0];
}
@Override
public String[] fileList() {
return new String[0];
}
@Override
public File getDir(String name, int mode) {
return null;
}
@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory) {
return null;
}
@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory, @Nullable DatabaseErrorHandler errorHandler) {
return null;
}
@Override
public boolean moveDatabaseFrom(Context sourceContext, String name) {
return false;
}
@Override
public boolean deleteDatabase(String name) {
return false;
}
@Override
public File getDatabasePath(String name) {
return null;
}
@Override
public String[] databaseList() {
return new String[0];
}
@Override
public Drawable getWallpaper() {
return null;
}
@Override
public Drawable peekWallpaper() {
return null;
}
@Override
public int getWallpaperDesiredMinimumWidth() {
return 0;
}
@Override
public int getWallpaperDesiredMinimumHeight() {
return 0;
}
@Override
public void setWallpaper(Bitmap bitmap) throws IOException {
}
@Override
public void setWallpaper(InputStream data) throws IOException {
}
@Override
public void clearWallpaper() throws IOException {
}
@Override
public void startActivity(Intent intent) {
}
@Override
public void startActivity(Intent intent, @Nullable Bundle options) {
}
@Override
public void startActivities(Intent[] intents) {
}
@Override
public void startActivities(Intent[] intents, Bundle options) {
}
@Override
public void startIntentSender(IntentSender intent, @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) throws IntentSender.SendIntentException {
}
@Override
public void startIntentSender(IntentSender intent, @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, @Nullable Bundle options) throws IntentSender.SendIntentException {
}
@Override
public void sendBroadcast(Intent intent) {
}
@Override
public void sendBroadcast(Intent intent, @Nullable String receiverPermission) {
}
@Override
public void sendOrderedBroadcast(Intent intent, @Nullable String receiverPermission) {
}
@Override
public void sendOrderedBroadcast(@NonNull Intent intent, @Nullable String receiverPermission, @Nullable BroadcastReceiver resultReceiver, @Nullable Handler scheduler, int initialCode, @Nullable String initialData, @Nullable Bundle initialExtras) {
}
@Override
public void sendBroadcastAsUser(Intent intent, UserHandle user) {
}
@Override
public void sendBroadcastAsUser(Intent intent, UserHandle user, @Nullable String receiverPermission) {
}
@Override
public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, @Nullable String receiverPermission, BroadcastReceiver resultReceiver, @Nullable Handler scheduler, int initialCode, @Nullable String initialData, @Nullable Bundle initialExtras) {
}
@Override
public void sendStickyBroadcast(Intent intent) {
}
@Override
public void sendStickyOrderedBroadcast(Intent intent, BroadcastReceiver resultReceiver, @Nullable Handler scheduler, int initialCode, @Nullable String initialData, @Nullable Bundle initialExtras) {
}
@Override
public void removeStickyBroadcast(Intent intent) {
}
@Override
public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
}
@Override
public void sendStickyOrderedBroadcastAsUser(Intent intent, UserHandle user, BroadcastReceiver resultReceiver, @Nullable Handler scheduler, int initialCode, @Nullable String initialData, @Nullable Bundle initialExtras) {
}
@Override
public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
}
@Nullable
@Override
public Intent registerReceiver(@Nullable BroadcastReceiver receiver, IntentFilter filter) {
return null;
}
@Nullable
@Override
public Intent registerReceiver(@Nullable BroadcastReceiver receiver, IntentFilter filter, int flags) {
return null;
}
@Nullable
@Override
public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, @Nullable String broadcastPermission, @Nullable Handler scheduler) {
return null;
}
@Nullable
@Override
public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, @Nullable String broadcastPermission, @Nullable Handler scheduler, int flags) {
return null;
}
@Override
public void unregisterReceiver(BroadcastReceiver receiver) {
}
@Nullable
@Override
public ComponentName startService(Intent service) {
return null;
}
@Nullable
@Override
public ComponentName startForegroundService(Intent service) {
return null;
}
@Override
public boolean stopService(Intent service) {
return false;
}
@Override
public boolean bindService(Intent service, @NonNull ServiceConnection conn, int flags) {
return false;
}
@Override
public void unbindService(@NonNull ServiceConnection conn) {
}
@Override
public boolean startInstrumentation(@NonNull ComponentName className, @Nullable String profileFile, @Nullable Bundle arguments) {
return false;
}
@Nullable
@Override
public Object getSystemService(@NonNull String name) {
return null;
}
@Nullable
@Override
public String getSystemServiceName(@NonNull Class<?> serviceClass) {
return null;
}
@Override
public int checkPermission(@NonNull String permission, int pid, int uid) {
return 0;
}
@Override
public int checkCallingPermission(@NonNull String permission) {
return 0;
}
@Override
public int checkCallingOrSelfPermission(@NonNull String permission) {
return 0;
}
@Override
public int checkSelfPermission(@NonNull String permission) {
return 0;
}
@Override
public void enforcePermission(@NonNull String permission, int pid, int uid, @Nullable String message) {
}
@Override
public void enforceCallingPermission(@NonNull String permission, @Nullable String message) {
}
@Override
public void enforceCallingOrSelfPermission(@NonNull String permission, @Nullable String message) {
}
@Override
public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
}
@Override
public void revokeUriPermission(Uri uri, int modeFlags) {
}
@Override
public void revokeUriPermission(String toPackage, Uri uri, int modeFlags) {
}
@Override
public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
return 0;
}
@Override
public int checkCallingUriPermission(Uri uri, int modeFlags) {
return 0;
}
@Override
public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
return 0;
}
@Override
public int checkUriPermission(@Nullable Uri uri, @Nullable String readPermission, @Nullable String writePermission, int pid, int uid, int modeFlags) {
return 0;
}
@Override
public void enforceUriPermission(Uri uri, int pid, int uid, int modeFlags, String message) {
}
@Override
public void enforceCallingUriPermission(Uri uri, int modeFlags, String message) {
}
@Override
public void enforceCallingOrSelfUriPermission(Uri uri, int modeFlags, String message) {
}
@Override
public void enforceUriPermission(@Nullable Uri uri, @Nullable String readPermission, @Nullable String writePermission, int pid, int uid, int modeFlags, @Nullable String message) {
}
@Override
public Context createPackageContext(String packageName, int flags) throws PackageManager.NameNotFoundException {
return null;
}
@Override
public Context createContextForSplit(String splitName) throws PackageManager.NameNotFoundException {
return null;
}
@Override
public Context createConfigurationContext(@NonNull Configuration overrideConfiguration) {
return null;
}
@Override
public Context createDisplayContext(@NonNull Display display) {
return null;
}
@Override
public Context createDeviceProtectedStorageContext() {
return null;
}
@Override
public boolean isDeviceProtectedStorage() {
return false;
}
};
static List<SWScreen> screens = new ArrayList<>(); static List<SWScreen> screens = new ArrayList<>();
public static List<SWScreen> getScreens() { public static List<SWScreen> getScreens() {
@ -592,7 +35,16 @@ public class SWDefinition {
SWDefinition() { SWDefinition() {
// List all the screens here // List all the screens here
// todo: SWValidator ?!? // todo: SWValidator ?!?
add(new SWScreen(R.string.nsclientinternal_title) add(new SWScreen(R.string.language)
.skippable(false)
.add(new SWRadioButton().option(R.array.languagesArray, R.array.languagesValues).preferenceId(R.string.key_language).label(R.string.language).comment(R.string.setupwizard_language_prompt))
.validator(() -> {
context = MainApp.instance().getApplicationContext();
LocaleHelper.setLocale(context, SP.getString(R.string.key_language, "en"));
MainApp.bus().post(new EventRefreshGui(true));
return SP.contains(R.string.key_language);}
))
.add(new SWScreen(R.string.nsclientinternal_title)
.skippable(true) .skippable(true)
.add(new SWUrl().preferenceId(R.string.key_nsclientinternal_url).label(R.string.nsclientinternal_url_title).comment(R.string.nsclientinternal_url_dialogmessage)) .add(new SWUrl().preferenceId(R.string.key_nsclientinternal_url).label(R.string.nsclientinternal_url_title).comment(R.string.nsclientinternal_url_dialogmessage))
.add(new SWString().preferenceId(R.string.key_nsclientinternal_api_secret).label(R.string.nsclientinternal_secret_dialogtitle).comment(R.string.nsclientinternal_secret_dialogmessage)) .add(new SWString().preferenceId(R.string.key_nsclientinternal_api_secret).label(R.string.nsclientinternal_secret_dialogtitle).comment(R.string.nsclientinternal_secret_dialogmessage))
@ -603,11 +55,7 @@ public class SWDefinition {
.add(new SWRadioButton().option(R.array.ageArray, R.array.ageValues).preferenceId(R.string.key_age).label(R.string.patientage).comment(R.string.patientage_summary)) .add(new SWRadioButton().option(R.array.ageArray, R.array.ageValues).preferenceId(R.string.key_age).label(R.string.patientage).comment(R.string.patientage_summary))
.validator(() -> SP.contains(R.string.key_age)) .validator(() -> SP.contains(R.string.key_age))
) )
.add(new SWScreen(R.string.language)
.skippable(false)
.add(new SWRadioButton().option(R.array.languagesArray, R.array.languagesValues).preferenceId(R.string.key_language).label(R.string.language).comment(R.string.setupwizard_language_prompt))
.validator(() -> {/*context = context.getApplicationContext(); LocaleHelper.setLocale(context, SP.getString(R.string.key_language, "en")); return*/ return SP.contains(R.string.key_language);}
))
; ;
} }

View file

@ -1,8 +1,6 @@
package info.nightscout.androidaps.startupwizard; package info.nightscout.androidaps.startupwizard;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.view.View; import android.view.View;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.RadioButton; import android.widget.RadioButton;
@ -50,7 +48,7 @@ public class SWRadioButton extends SWItem {
String[] values = context.getResources().getStringArray(valuesArray); String[] values = context.getResources().getStringArray(valuesArray);
// Get if there is already value in SP // Get if there is already value in SP
String previousValue = SP.getString(preferenceId, "unset"); String previousValue = SP.getString(preferenceId, "unset");
log.debug("Value for "+view.getContext().getString(preferenceId)+" is "+previousValue); // log.debug("Value for "+view.getContext().getString(preferenceId)+" is "+previousValue);
radioGroup = new RadioGroup(context); radioGroup = new RadioGroup(context);
radioGroup.clearCheck(); radioGroup.clearCheck();
@ -100,7 +98,6 @@ public class SWRadioButton extends SWItem {
public void save(){ public void save(){
if(!getCheckedValue().equals("none")) { if(!getCheckedValue().equals("none")) {
log.debug("Saving "+preferenceId+" value "+getCheckedValue());
SP.putString(preferenceId, getCheckedValue()); SP.putString(preferenceId, getCheckedValue());
MainApp.bus().post(new EventPreferenceChange(preferenceId)); MainApp.bus().post(new EventPreferenceChange(preferenceId));
} }

View file

@ -20,7 +20,10 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.utils.LocaleHelper;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;
import static info.nightscout.androidaps.startupwizard.SWItem.Type.RADIOBUTTON; import static info.nightscout.androidaps.startupwizard.SWItem.Type.RADIOBUTTON;
@ -120,7 +123,6 @@ public class SetupWizardActivity extends AppCompatActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setupwizard); setContentView(R.layout.activity_setupwizard);
mVisible = true; mVisible = true;
@ -193,6 +195,23 @@ public class SetupWizardActivity extends AppCompatActivity {
delayedHide(100); delayedHide(100);
} }
@Override
protected void onResume() {
super.onResume();
// check is current locale is different from the one in preferences
// log.debug("Current: "+LocaleHelper.getLanguage(this)+" preferences: "+SP.getString("language", "en"));
if(!LocaleHelper.getLanguage(this).equals(SP.getString("language", "en"))) {
// it is so change it in locale and restart SetupWizard
// log.debug("Setting locale to: "+SP.getString("language", "en")+" and restarting");
LocaleHelper.setLocale(this, SP.getString(R.string.key_language, "en"));
MainApp.bus().post(new EventRefreshGui(true));
Intent intent = getIntent();
this.finish();
startActivity(intent);
}
}
private void toggle() { private void toggle() {
if (mVisible) { if (mVisible) {
hide(); hide();
@ -265,4 +284,5 @@ public class SetupWizardActivity extends AppCompatActivity {
Intent intent = new Intent(this, MainActivity.class); Intent intent = new Intent(this, MainActivity.class);
startActivity(intent); startActivity(intent);
} }
} }