Skip to content

Commit

Permalink
Merge pull request #25 from axenuli/master
Browse files Browse the repository at this point in the history
More explicit locale choices on settings. Closes #24
  • Loading branch information
kueda committed Jan 17, 2015
2 parents e5d1beb + 2a0374c commit f68e016
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 92 deletions.
22 changes: 0 additions & 22 deletions res/layout/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,28 +141,6 @@
android:id="@+id/radioLang"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RadioButton
android:id="@+id/rbDeviceLang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/use_device_language_settings" />

<RadioButton
android:id="@+id/rbDeviceEu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/locale_eu" />

<RadioButton
android:id="@+id/rbDeviceGl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/locale_gl" />

</RadioGroup>
</LinearLayout>

Expand Down
157 changes: 87 additions & 70 deletions src/org/inaturalist/android/INaturalistPrefsActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.inaturalist.android;

import java.util.ArrayList;
import java.util.Locale;

import org.inaturalist.android.INaturalistService.LoginType;

import com.actionbarsherlock.app.ActionBar;
Expand Down Expand Up @@ -64,7 +66,8 @@ public class INaturalistPrefsActivity extends SherlockActivity {
private LoginButton mFacebookLoginButton;
private Button mGoogleLogin;
private View mFBSeparator;
private RadioGroup rbPreferredLocaleSelector;
private RadioGroup rbPreferredLocaleSelector;

private INaturalistApp mApp;

private UiLifecycleHelper mUiHelper;
Expand Down Expand Up @@ -116,8 +119,8 @@ public void onClick(DialogInterface dialog, int whichButton) {

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
Expand All @@ -134,8 +137,8 @@ protected void onCreate(Bundle savedInstanceState) {
mApp = (INaturalistApp) getApplicationContext();
}

ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);


Expand Down Expand Up @@ -176,15 +179,13 @@ protected void onCreate(Bundle savedInstanceState) {
mHelp = (TextView) findViewById(R.id.tutorial_link);
mHelp.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);

rbPreferredLocaleSelector = (RadioGroup)findViewById(R.id.radioLang);

RadioButton rbDeviceLanguage = (RadioButton)findViewById(R.id.rbDeviceLang);
rbDeviceLanguage.setText( rbDeviceLanguage.getText() + " (" + mApp.getFormattedDeviceLocale() + ")" );


makeLanguageRadioButtons();

mHelp.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(INaturalistPrefsActivity.this, TutorialActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Intent intent = new Intent(INaturalistPrefsActivity.this, TutorialActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("first_time", false);
startActivity(intent);
}
Expand Down Expand Up @@ -263,68 +264,84 @@ public void onClick(DialogInterface dialog, int which) {

updateRadioButtonState();
}

public void makeLanguageRadioButtons()
{
rbPreferredLocaleSelector = (RadioGroup)findViewById(R.id.radioLang);

String[] locales = LocaleHelper.SupportedLocales;
for (int i=0; i < locales.length; i++) {
RadioButton rb = new RadioButton(this);
final int selectedButton = i;
final Activity context = this;
rb.setText(new Locale(locales[i]).getDisplayLanguage());
rb.setOnClickListener (new OnClickListener() {
@Override
public void onClick(View v) {
PromptUserToConfirmSelection(context, selectedButton);
}
});
rbPreferredLocaleSelector.addView(rb, i);
}
}

private void PromptUserToConfirmSelection(Activity context, int index) {
final int selectedButton = index;
final String locale = LocaleHelper.SupportedLocales[index];
final Activity thisActivity = context;
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
mPrefEditor.putString("pref_locale", locale);
mPrefEditor.commit();
formerSelectedRadioButton = selectedButton;
mApp.applyLocaleSettings();
mApp.restart();
break;

case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
rbPreferredLocaleSelector.check(rbPreferredLocaleSelector.getChildAt(formerSelectedRadioButton).getId());
break;
}
}
};

AlertDialog.Builder builder = new AlertDialog.Builder(thisActivity);
builder.setMessage(getString(R.string.language_restart))
.setPositiveButton(getString(R.string.restart_now), dialogClickListener)
.setNegativeButton(getString(R.string.cancel), dialogClickListener).show();


}
private void updateRadioButtonState(){
String pref_locale = mPreferences.getString("pref_locale", "");
if(pref_locale.equalsIgnoreCase("eu")){
rbPreferredLocaleSelector.check(R.id.rbDeviceEu);
formerSelectedRadioButton = R.id.rbDeviceEu;
}else if(pref_locale.equalsIgnoreCase("gl")){
rbPreferredLocaleSelector.check(R.id.rbDeviceGl);
formerSelectedRadioButton = R.id.rbDeviceGl;
}else{
rbPreferredLocaleSelector.check(R.id.rbDeviceLang);
formerSelectedRadioButton = R.id.rbDeviceLang;
}
}

public void onRadioButtonClicked(View view){
final boolean checked = ((RadioButton) view).isChecked();
final int selectedRadioButtonId = view.getId();
//Toast.makeText(getApplicationContext(), getString(R.string.language_restart), Toast.LENGTH_LONG).show();

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
switch(selectedRadioButtonId) {
case R.id.rbDeviceEu:
if (checked){
mPrefEditor.putString("pref_locale", "eu");
mPrefEditor.commit();
}
break;
case R.id.rbDeviceGl:
if (checked){
mPrefEditor.putString("pref_locale", "gl");
mPrefEditor.commit();
}
break;
default:
if(checked){
mPrefEditor.putString("pref_locale", "");
mPrefEditor.commit();
}
break;
}
formerSelectedRadioButton = selectedRadioButtonId;
mApp.applyLocaleSettings();
mApp.restart();
break;

case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
rbPreferredLocaleSelector.check(formerSelectedRadioButton);
break;
}
}
};
String[] supportedLocales = LocaleHelper.SupportedLocales;

// if no preference is set, find app default
if (pref_locale.equalsIgnoreCase("")) {
String defaultLocale = LocaleHelper.getDefaultLocale();
for (int i = 0; i < supportedLocales.length; i++) {
if (supportedLocales[i].equalsIgnoreCase(defaultLocale)) {
RadioButton rb = (RadioButton) rbPreferredLocaleSelector.getChildAt(i);
rb.setChecked(true);
formerSelectedRadioButton = i;
return;
}
}
}
else {
for (int i = 0; i < supportedLocales.length; i++) {
if (pref_locale.equalsIgnoreCase(supportedLocales[i])) {
RadioButton rb = (RadioButton) rbPreferredLocaleSelector.getChildAt(i);
rb.setChecked(true);
formerSelectedRadioButton = i;
return;
}
}
}

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.language_restart))
.setPositiveButton(getString(R.string.restart_now), dialogClickListener)
.setNegativeButton(getString(R.string.cancel), dialogClickListener).show();
}

@Override
Expand Down
31 changes: 31 additions & 0 deletions src/org/inaturalist/android/LocaleHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.inaturalist.android;

import java.util.Locale;

/**
* Created by ulrikeaxen on 1/4/15.
*/
public class LocaleHelper {

private static final String DefaultLocale = "en";

// add locale codes here when they are complete in values
// Todo: what is es-Res?
public static final String[] SupportedLocales = new String[] {
"en",
"ca",
"es",
"eu",
"gl"
};


public static String getDefaultLocale()
{
String languageCode = Locale.getDefault().getLanguage();
for (int i = 0; i < SupportedLocales.length; i++)
if (languageCode.equalsIgnoreCase(SupportedLocales[i]))
return SupportedLocales[i];
return DefaultLocale;
}
}

0 comments on commit f68e016

Please sign in to comment.