From f03f259b0ede350071f115d8385dc1cc2811cb52 Mon Sep 17 00:00:00 2001 From: baksosapi Date: Wed, 8 Nov 2017 08:43:41 +0800 Subject: [PATCH] WD|Fix switch language. #4 --- .../bidan/activity/LoginActivity.java | 105 ++++++++---------- 1 file changed, 48 insertions(+), 57 deletions(-) diff --git a/opensrp-bidan/src/main/java/org/smartregister/bidan/activity/LoginActivity.java b/opensrp-bidan/src/main/java/org/smartregister/bidan/activity/LoginActivity.java index bde602a..8bc1743 100644 --- a/opensrp-bidan/src/main/java/org/smartregister/bidan/activity/LoginActivity.java +++ b/opensrp-bidan/src/main/java/org/smartregister/bidan/activity/LoginActivity.java @@ -77,18 +77,52 @@ import static org.smartregister.util.Log.logVerbose; public class LoginActivity extends AppCompatActivity { - private static final String TAG = LoginActivity.class.getName(); - private EditText userNameEditText; - private EditText passwordEditText; - private ProgressDialog progressDialog; public static final String ENGLISH_LOCALE = "en"; + private static final String TAG = LoginActivity.class.getName(); private static final String BAHASA_LOCALE = "in"; private static final String URDU_LOCALE = "ur"; private static final String ENGLISH_LANGUAGE = "English"; private static final String URDU_LANGUAGE = "Urdu"; + private EditText userNameEditText; + private EditText passwordEditText; + private ProgressDialog progressDialog; private android.content.Context appContext; private RemoteLoginTask remoteLoginTask; + public static void setLanguage() { + AllSharedPreferences allSharedPreferences = new AllSharedPreferences(getDefaultSharedPreferences(getOpenSRPContext().applicationContext())); + String preferredLocale = allSharedPreferences.fetchLanguagePreference(); + Resources res = getOpenSRPContext().applicationContext().getResources(); + // Change locale settings in the app. + DisplayMetrics dm = res.getDisplayMetrics(); + Configuration conf = res.getConfiguration(); + conf.locale = new Locale(preferredLocale); + res.updateConfiguration(conf, dm); + + } + + public static String switchLanguagePreference() { + AllSharedPreferences allSharedPreferences = new AllSharedPreferences(getDefaultSharedPreferences(getOpenSRPContext().applicationContext())); + + String preferredLocale = allSharedPreferences.fetchLanguagePreference(); + if (!BAHASA_LOCALE.equals(preferredLocale)) { + allSharedPreferences.saveLanguagePreference(BAHASA_LOCALE); + } else { + allSharedPreferences.saveLanguagePreference(ENGLISH_LOCALE); + } + Resources res = getOpenSRPContext().applicationContext().getResources(); + // Change locale settings in the app. + DisplayMetrics dm = res.getDisplayMetrics(); + android.content.res.Configuration conf = res.getConfiguration(); + conf.locale = new Locale(preferredLocale); + res.updateConfiguration(conf, dm); + return preferredLocale; + } + + public static Context getOpenSRPContext() { + return BidanApplication.getInstance().context(); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -108,7 +142,7 @@ public void onCreate(Bundle savedInstanceState) { } setContentView(R.layout.login); - ImageView loginglogo = (ImageView)findViewById(R.id.login_logo); + ImageView loginglogo = (ImageView) findViewById(R.id.login_logo); loginglogo.setImageDrawable(getResources().getDrawable(R.drawable.login_logo_bidan)); getSupportActionBar().setDisplayShowTitleEnabled(false); @@ -126,7 +160,7 @@ public void onCreate(Bundle savedInstanceState) { setLanguage(); - if (BuildConfig.DEBUG){ + if (BuildConfig.DEBUG) { debugApp(); } @@ -153,15 +187,15 @@ private void debugApp() { Config config = new Config(); String uname = null, pwd = null; try { - uname = config.getCredential("uname", getApplicationContext()); - pwd = config.getCredential("pwd", getApplicationContext()); + uname = config.getCredential("uname", getApplicationContext()); + pwd = config.getCredential("pwd", getApplicationContext()); } catch (IOException e) { e.printStackTrace(); } LayoutInflater layoutInflater = getLayoutInflater(); View view = layoutInflater.inflate(R.layout.login, null); - if (BidanApplication.getInstance().context().userService().hasARegisteredUser()){ + if (BidanApplication.getInstance().context().userService().hasARegisteredUser()) { localLogin(view, uname, pwd); } else { remoteLogin(view, uname, pwd); @@ -247,7 +281,6 @@ private void localLogin(View view, String userName, String password) { } } - private void remoteLogin(final View view, final String userName, final String password) { if (!getOpenSRPContext().allSharedPreferences().fetchBaseURL("").isEmpty()) { @@ -368,6 +401,11 @@ private void tryRemoteLogin(final String userName, final String password, final remoteLoginTask.execute(); } +// private void startZScoreIntentService() { +// Intent intent = new Intent(this, ZScoreRefreshIntentService.class); +// startService(intent); +// } + private void fillUserIfExists() { if (getOpenSRPContext().userService().hasARegisteredUser()) { userNameEditText.setText(getOpenSRPContext().allSharedPreferences().fetchRegisteredANM()); @@ -398,11 +436,6 @@ public void run() { }).start(); } -// private void startZScoreIntentService() { -// Intent intent = new Intent(this, ZScoreRefreshIntentService.class); -// startService(intent); -// } - private void remoteLoginWith(String userName, String password, String userInfo) { getOpenSRPContext().userService().remoteLogin(userName, password, userInfo); goToHome(true); @@ -417,7 +450,6 @@ private void goToHome(boolean remote) { } BidanApplication.setCrashlyticsUser(getOpenSRPContext()); // Intent intent = new Intent(this, ChildSmartRegisterActivity.class); -// Intent intent = new Intent(this, BidanHomeActivity.class); Intent intent = new Intent(this, BidanHomeActivity.class); intent.putExtra(BaseRegisterActivity.IS_REMOTE_LOGIN, remote); startActivity(intent); @@ -439,43 +471,6 @@ private String getBuildDate() throws PackageManager.NameNotFoundException, IOExc return new SimpleDateFormat("dd MMM yyyy", Locale.getDefault()).format(new java.util.Date(ze.getTime())); } - public static void setLanguage() { - AllSharedPreferences allSharedPreferences = new AllSharedPreferences(getDefaultSharedPreferences(getOpenSRPContext().applicationContext())); - String preferredLocale = allSharedPreferences.fetchLanguagePreference(); - Resources res = getOpenSRPContext().applicationContext().getResources(); - // Change locale settings in the app. - DisplayMetrics dm = res.getDisplayMetrics(); - Configuration conf = res.getConfiguration(); - conf.locale = new Locale(preferredLocale); - res.updateConfiguration(conf, dm); - - } - - public static String switchLanguagePreference() { - AllSharedPreferences allSharedPreferences = new AllSharedPreferences(getDefaultSharedPreferences(getOpenSRPContext().applicationContext())); - - String preferredLocale = allSharedPreferences.fetchLanguagePreference(); - if (BAHASA_LOCALE.equals(preferredLocale)) { - allSharedPreferences.saveLanguagePreference(BAHASA_LOCALE); - Resources res = getOpenSRPContext().applicationContext().getResources(); - // Change locale settings in the app. - DisplayMetrics dm = res.getDisplayMetrics(); - android.content.res.Configuration conf = res.getConfiguration(); - conf.locale = new Locale(BAHASA_LOCALE); - res.updateConfiguration(conf, dm); - return BAHASA_LOCALE; - } else { - allSharedPreferences.saveLanguagePreference(ENGLISH_LOCALE); - Resources res = getOpenSRPContext().applicationContext().getResources(); - // Change locale settings in the app. - DisplayMetrics dm = res.getDisplayMetrics(); - android.content.res.Configuration conf = res.getConfiguration(); - conf.locale = new Locale(ENGLISH_LOCALE); - res.updateConfiguration(conf, dm); - return ENGLISH_LANGUAGE; - } - } - private void positionViews() { final ScrollView canvasSV = (ScrollView) findViewById(R.id.canvasSV); if (canvasSV == null) { @@ -507,10 +502,6 @@ public void onGlobalLayout() { }); } - public static Context getOpenSRPContext() { - return BidanApplication.getInstance().context(); - } - private void extractLocations(ArrayList locationList, JSONObject rawLocationData) throws JSONException { final String NODE = "node";