From bf4b9269fed0bb71cb14c55f062f15eb3f1ae66e Mon Sep 17 00:00:00 2001 From: richard Date: Fri, 20 Nov 2020 21:07:25 +0300 Subject: [PATCH 1/3] Fix change locale not working after migratio to androidx bug --- opensrp-app/build.gradle | 2 +- .../view/activity/MultiLanguageActivity.java | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/opensrp-app/build.gradle b/opensrp-app/build.gradle index ab514892b..9d7ede221 100644 --- a/opensrp-app/build.gradle +++ b/opensrp-app/build.gradle @@ -164,7 +164,7 @@ dependencies { implementation 'net.zetetic:android-database-sqlcipher:4.2.0' implementation 'androidx.multidex:multidex:2.0.1' implementation 'org.codehaus.jackson:jackson-core-asl:1.9.13' - implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation "org.apache.httpcomponents:httpmime:4.5.6" implementation group: 'commons-codec', name: 'commons-codec', version: '1.10' diff --git a/opensrp-app/src/main/java/org/smartregister/view/activity/MultiLanguageActivity.java b/opensrp-app/src/main/java/org/smartregister/view/activity/MultiLanguageActivity.java index 07587f3ee..9cdab8053 100644 --- a/opensrp-app/src/main/java/org/smartregister/view/activity/MultiLanguageActivity.java +++ b/opensrp-app/src/main/java/org/smartregister/view/activity/MultiLanguageActivity.java @@ -18,4 +18,17 @@ protected void attachBaseContext(android.content.Context base) { super.attachBaseContext(LangUtils.setAppLocale(base, lang)); } + //solution borrowed from + // https://stackoverflow.com/questions/55265834/change-locale-not-work-after-migrate-to-androidx/61420643#61420643 + @Override + public void applyOverrideConfiguration(Configuration overrideConfiguration) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && + Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + // add this to fix androidx.appcompat:appcompat 1.1.0 bug + // which happens on Android 6.x ~ 7.x + getResources(); + } + super.applyOverrideConfiguration(overrideConfiguration); + } + } From 03ef5dddc4f365238d77abbfaa37d1e3fc097a4a Mon Sep 17 00:00:00 2001 From: richard Date: Fri, 20 Nov 2020 21:21:34 +0300 Subject: [PATCH 2/3] Fix build error --- gradle.properties | 2 +- opensrp-app/build.gradle | 2 +- .../org/smartregister/view/activity/MultiLanguageActivity.java | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index 39246ac47..d8a67c0e6 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=3.3.1-SNAPSHOT +VERSION_NAME=3.3.2-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Core Application diff --git a/opensrp-app/build.gradle b/opensrp-app/build.gradle index 9d7ede221..ab514892b 100644 --- a/opensrp-app/build.gradle +++ b/opensrp-app/build.gradle @@ -164,7 +164,7 @@ dependencies { implementation 'net.zetetic:android-database-sqlcipher:4.2.0' implementation 'androidx.multidex:multidex:2.0.1' implementation 'org.codehaus.jackson:jackson-core-asl:1.9.13' - implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation "org.apache.httpcomponents:httpmime:4.5.6" implementation group: 'commons-codec', name: 'commons-codec', version: '1.10' diff --git a/opensrp-app/src/main/java/org/smartregister/view/activity/MultiLanguageActivity.java b/opensrp-app/src/main/java/org/smartregister/view/activity/MultiLanguageActivity.java index 9cdab8053..e907e5d3f 100644 --- a/opensrp-app/src/main/java/org/smartregister/view/activity/MultiLanguageActivity.java +++ b/opensrp-app/src/main/java/org/smartregister/view/activity/MultiLanguageActivity.java @@ -1,5 +1,8 @@ package org.smartregister.view.activity; +import android.content.res.Configuration; +import android.os.Build; + import androidx.appcompat.app.AppCompatActivity; import org.smartregister.util.LangUtils; From 528ca629cc94b37b8c73e5bcdc2373721083fffa Mon Sep 17 00:00:00 2001 From: Martin Ndegwa Date: Wed, 25 Nov 2020 22:00:41 +0300 Subject: [PATCH 3/3] =?UTF-8?q?Refactor=20language=20switcher=20implementa?= =?UTF-8?q?tion=20=E2=99=BB=EF=B8=8F=20-=20Fixes=20dynamic=20language=20sw?= =?UTF-8?q?itching=20no=20longer=20works?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- opensrp-app/build.gradle | 2 +- .../org/smartregister/util/LangUtils.java | 31 ++++++++++++++----- .../view/activity/BarcodeScanActivity.java | 8 +++-- .../view/activity/MultiLanguageActivity.java | 23 +++++--------- .../view/activity/SettingsActivity.java | 8 ++++- 6 files changed, 47 insertions(+), 27 deletions(-) diff --git a/gradle.properties b/gradle.properties index d8a67c0e6..c33871539 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=3.3.2-SNAPSHOT +VERSION_NAME=4.0.0-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Core Application diff --git a/opensrp-app/build.gradle b/opensrp-app/build.gradle index ab514892b..9d7ede221 100644 --- a/opensrp-app/build.gradle +++ b/opensrp-app/build.gradle @@ -164,7 +164,7 @@ dependencies { implementation 'net.zetetic:android-database-sqlcipher:4.2.0' implementation 'androidx.multidex:multidex:2.0.1' implementation 'org.codehaus.jackson:jackson-core-asl:1.9.13' - implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation "org.apache.httpcomponents:httpmime:4.5.6" implementation group: 'commons-codec', name: 'commons-codec', version: '1.10' diff --git a/opensrp-app/src/main/java/org/smartregister/util/LangUtils.java b/opensrp-app/src/main/java/org/smartregister/util/LangUtils.java index b2decf860..4532f402c 100644 --- a/opensrp-app/src/main/java/org/smartregister/util/LangUtils.java +++ b/opensrp-app/src/main/java/org/smartregister/util/LangUtils.java @@ -3,6 +3,8 @@ import android.content.Context; import android.content.res.Configuration; import android.content.res.Resources; +import android.os.Build; +import android.os.LocaleList; import android.preference.PreferenceManager; import org.smartregister.repository.AllSharedPreferences; @@ -25,19 +27,34 @@ public static String getLanguage(Context ctx) { return allSharedPreferences.fetchLanguagePreference(); } - public static Context setAppLocale(Context context, String language) { - Locale locale = new Locale(language); - + public static Configuration setAppLocale(Context context, String language) { Resources res = context.getResources(); - Configuration conf = res.getConfiguration(); - conf.setLocale(locale); + Configuration configuration = res.getConfiguration(); + try { - context = context.createConfigurationContext(conf); + Locale locale = new Locale(language); + + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + configuration.setLocale(locale); + + LocaleList localeList = new LocaleList(locale); + LocaleList.setDefault(localeList); + configuration.setLocales(localeList); + + context.createConfigurationContext(configuration); + + } else { + configuration.locale = locale; + res.updateConfiguration(configuration, res.getDisplayMetrics()); + } + + } catch (Exception e) { Timber.e(e); } - return context; + return configuration; } } diff --git a/opensrp-app/src/main/java/org/smartregister/view/activity/BarcodeScanActivity.java b/opensrp-app/src/main/java/org/smartregister/view/activity/BarcodeScanActivity.java index 11ab66b19..8c4936197 100644 --- a/opensrp-app/src/main/java/org/smartregister/view/activity/BarcodeScanActivity.java +++ b/opensrp-app/src/main/java/org/smartregister/view/activity/BarcodeScanActivity.java @@ -6,6 +6,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.res.Configuration; import android.os.Bundle; import android.os.Vibrator; import android.util.SparseArray; @@ -31,12 +32,15 @@ public class BarcodeScanActivity extends Activity implements Detector.Processor< private CameraSource cameraSource; private CameraSourcePreview cameraSourcePreview; - @Override protected void attachBaseContext(Context base) { // get language from prefs String lang = LangUtils.getLanguage(base.getApplicationContext()); - super.attachBaseContext(LangUtils.setAppLocale(base, lang)); + Configuration newConfiguration = LangUtils.setAppLocale(base, lang); + + super.attachBaseContext(base); + + applyOverrideConfiguration(newConfiguration); } @Override diff --git a/opensrp-app/src/main/java/org/smartregister/view/activity/MultiLanguageActivity.java b/opensrp-app/src/main/java/org/smartregister/view/activity/MultiLanguageActivity.java index e907e5d3f..0886edc49 100644 --- a/opensrp-app/src/main/java/org/smartregister/view/activity/MultiLanguageActivity.java +++ b/opensrp-app/src/main/java/org/smartregister/view/activity/MultiLanguageActivity.java @@ -1,14 +1,15 @@ package org.smartregister.view.activity; import android.content.res.Configuration; -import android.os.Build; import androidx.appcompat.app.AppCompatActivity; import org.smartregister.util.LangUtils; -/** Use this class to ensure your activities have multi-language support +/** + * Use this class to ensure your activities have multi-language support + * * @author Rodgers Andati * @since 2019-05-03 */ @@ -18,20 +19,12 @@ public class MultiLanguageActivity extends AppCompatActivity { protected void attachBaseContext(android.content.Context base) { // get language from prefs String lang = LangUtils.getLanguage(base.getApplicationContext()); - super.attachBaseContext(LangUtils.setAppLocale(base, lang)); - } + Configuration newConfiguration = LangUtils.setAppLocale(base, lang); - //solution borrowed from - // https://stackoverflow.com/questions/55265834/change-locale-not-work-after-migrate-to-androidx/61420643#61420643 - @Override - public void applyOverrideConfiguration(Configuration overrideConfiguration) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && - Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { - // add this to fix androidx.appcompat:appcompat 1.1.0 bug - // which happens on Android 6.x ~ 7.x - getResources(); - } - super.applyOverrideConfiguration(overrideConfiguration); + super.attachBaseContext(base); + + applyOverrideConfiguration(newConfiguration); } + } diff --git a/opensrp-app/src/main/java/org/smartregister/view/activity/SettingsActivity.java b/opensrp-app/src/main/java/org/smartregister/view/activity/SettingsActivity.java index 1839e8e47..d83f2a023 100644 --- a/opensrp-app/src/main/java/org/smartregister/view/activity/SettingsActivity.java +++ b/opensrp-app/src/main/java/org/smartregister/view/activity/SettingsActivity.java @@ -3,6 +3,7 @@ import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; +import android.content.res.Configuration; import android.os.Bundle; import android.preference.EditTextPreference; import android.preference.Preference; @@ -32,8 +33,13 @@ public class SettingsActivity extends PreferenceActivity implements Preference.O @Override protected void attachBaseContext(android.content.Context base) { // get language from prefs + String lang = LangUtils.getLanguage(base.getApplicationContext()); - super.attachBaseContext(LangUtils.setAppLocale(base, lang)); + Configuration newConfiguration = LangUtils.setAppLocale(base, lang); + + super.attachBaseContext(base); + + applyOverrideConfiguration(newConfiguration); } @Override