Skip to content

Commit

Permalink
WD|Fix switch language. #4
Browse files Browse the repository at this point in the history
  • Loading branch information
baksosapi committed Nov 8, 2017
1 parent ee54679 commit f03f259
Showing 1 changed file with 48 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -126,7 +160,7 @@ public void onCreate(Bundle savedInstanceState) {

setLanguage();

if (BuildConfig.DEBUG){
if (BuildConfig.DEBUG) {
debugApp();
}

Expand All @@ -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);
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -507,10 +502,6 @@ public void onGlobalLayout() {
});
}

public static Context getOpenSRPContext() {
return BidanApplication.getInstance().context();
}

private void extractLocations(ArrayList<String> locationList, JSONObject rawLocationData)
throws JSONException {
final String NODE = "node";
Expand Down

0 comments on commit f03f259

Please sign in to comment.